Ilya Smagin
Ilya Smagin

Reputation: 6152

Is there any common way to save application settings in more advanced way, than plain .settings file?

There's a large WPF application with several modules, windows, self-written controls, etc. We need to persist state for some of elements of user interface. For example:

.Settings file seems too plain for this because of no hierarchy in it. Why can't I just serialize some SettingsModel, containing everything I need and then restore it on application startup? The very important requirement for persistence mechanism is that it shoud be extensible: If I refactor settings structure, and will try to de-serialize the file created with some previous version of SettingsModel class, I will obviously fail.

So the quiestion is: are there any frameworks for persisting complex settings?

Upvotes: 8

Views: 586

Answers (4)

Ed Power
Ed Power

Reputation: 8531

I've done this by writing my own serialization code to XML, labeling the elements to match the configuration fields. When I deserialize, I query the XML for each element I want to populate. If I'm deserializing an old version into a new config scheme that has additional elements, the XML query returns null and I instead insert a default value. It lets me handle lists of hierarchical data, I can encrypt any portion of it I need, and I don't version the XML. Although its a bit more work than using XMLSerializer, my config data doesn't change very often so it was worth it.

Since you can have lots of users, you can save each user's XML as a string in a database. System.Data.Sqlite, RaptorDb, and FileDb work well for this, as does PersistentDictionary.

Yet another alternative is to store your data in dictionaries of dictionaries and use SharpSerializer to save it as XML to either a file or one of the above databases.

Upvotes: 1

Scott Chamberlain
Scott Chamberlain

Reputation: 127563

The .Settings file supports changing the structure over time. You don't even need to use the Settings.cs file you can make your own settings providers and have them populate from the config file, each with their own customized Upgrade method to handle changes in the structure.

Upvotes: 2

brunnerh
brunnerh

Reputation: 184506

As Rachel suggested, you could use XML serialization, i for one always use that for settings, it has some tolerance for changes but i do not know if it would fit all your needs.

Upvotes: 3

deltree
deltree

Reputation: 3824

app.config is another common storage location. Config file settings can be accessed easily from the application and you can even build your own custom configSections

Upvotes: 1

Related Questions