Application Settings for C # Application

I have an application that has settings that the user can select before starting the application. Settings are saved in the database. The entire application uses these settings. Right now, every class that uses settings calls a database in its constructor to load the settings into the class. This seems strange to me, because the settings should not change in the middle of the application launch. So what do you call your application settings? Are you using a static class or singleton pattern instead of hitting the database each time to invoke the same settings?

+4
source share
4 answers

Yes, I usually throw such things into the static Application class, especially if there is no reason to request several times.

+5
source

Your classes depend on the settings. Thus, you may want the Injection of Dependency to separate them from how they are stored and improve the testability of your classes.

This is much simpler if you use the dependency injection infrastructure (Castle Windsor, NInject, etc.).

To avoid re-querying the database, you must create a Settings object that has a Singleton lifetime. Avoid a static singleton such as a plague. They make your application inherently inappropriate. See Static Singlets - Anti-pattern .

+2
source

I will go with Singleton, who is actually just a more controlled modification of Jarrett's answer. Go with which your design fits.

And ignore all the bad press about Singleton. Absolutely any design can be abused, just use it wisely.

NTN

+1
source

As you already said, this responsibility is usually put on a singleton class that contains application data.

Another option is to use the "Settings" tab in the project properties in VS2005 / 2008/2010. (Right-click on the project name, and then go to the Settings tab and create a settings file.) For more information about the Settings page: http://msdn.microsoft.com/en-us/library/cftf714c (VS.90) .aspx

You can automatically change the settings page (from the database) when the application starts, and then read the values ​​from anywhere in the application. See this post for more information on ConfigurationManager Class http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

Good luck

+1
source

All Articles