Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to provide software settings?

Tags:

c#

.net

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?

like image 258
claws Avatar asked Mar 31 '10 19:03

claws


2 Answers

.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.

like image 117
womp Avatar answered Sep 19 '22 05:09

womp


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!

like image 23
Jeffrey L Whitledge Avatar answered Sep 22 '22 05:09

Jeffrey L Whitledge