开发者

Best way to provide software settings?

I'm using C# .NET.

In my software I'm providing settings dialog through which user can set the application settings which I want to save to a file.

Requirements (typical):

  1. Every class I defined uses some part of these settings. So, these should be global to all classes.
  2. These should be loaded while software is started.
  3. When ever user changes settings and clicks 'save'/'apply'. Current settings开发者_开发知识库 should change.

I am wondering what is the best way to do this? Also, what is the best way to save these settings to disk? I mean should I create a Settings class object and serializing it to 'settings.dat' or provide an structured file like XML/JSON

This is required for almost every other software. So, isn't there any design pattern for this?

EDIT:

Well, thats something that I didn't know. Its nice :). But say while user is using the software in the middle he changes the settings then all the other objects that are using these global Properties.Settings.Default.* should be changed. Is there any kind of notification mechanism? Some kind of event?


.Net projects already have the notion of Settings, scoped to either the user or the application, that will meet all of your requirements above. There are classes to read and write the settings. I would highly recommend that you look at these instead of rolling something up yourself.

Using Settings in C#

You can use Settings in a variety of project types, although in certain types of projects like ASP.Net projects, User-level settings may not be available.


The Settings class that comes with .Net is very handy, and I use it for most of my projects. The one gotcha to watch out for is that every new version of the application gets its own settings file, so make sure you have sensable defaults. All the settings will disappear whenever a new EXE is distributed.

Global state is very hard to deal with correctly, so I usually pass the relevant settings to the various objects in their constructors, or in properties. And I usually don't apply settings changes to those objects, since, in many cases, it's very hard for an object to deal with a changing setting intelligently. Rather, I just use the new settings for the new objects as they are created. If a setting needs to be applied immediately, then I just dump the old object and create a new one. It just depends on the details of the application.

If you have an Apply button on your settings screen, then I would recommend reloading and displaying all of the values after saving them. This way the display is sure to contain exactly what is actually saved. This could be important if any settings are parsed. I've had users enter a month and day combination into a particular field, and the format they used was different from what was expected, so the value saved was incorrect. By updating the screen after the Apply, these sorts of errors can be made obvious.

I hope this helps!


You and womp are both right:

You should create a Settings class that is a Facade over the .NET settings. That way you get the best of both worlds: the testability of a hand-rolled solution and the ease of implementation typically associated with Microsoft Silver Bullets.


I personally would go the Properties.Settings route. Add a Settings file to the Properties folder of your app. This lets you easily add items to the app.config file of your application. There is a built in .net class that you can use to read/write values found in the Settings file. You can then write a small wrapper class that encapsulates that functionality or simply use the built in .net one all over the place.

I personally would create a threadsafe Singleton class that uses the small wrapper class over the built in .net one. Yes it's some extra work but it's a small amount and gives you some great power in your app for the little bit of work.

Edit: Sorry forgot to include the MSDN settings link.

http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx


As others have suggested using the Settings feature is the way to go. Settings also provide events when the they are updated so you can handle them and take necessary actions. You can also use the Upgrade method to move the settings from previous version into the newer one. Just make sure that you call it only once (probably from the installer).

Settings can also be bounded to the controls so you will not have to map them manually from controls to settings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜