tags:

views:

135

answers:

4

Hello,

I have an app that has settings the user can pick before running the app. The settings are stored in a database. The entire app uses these settings. Right now, each class that uses the settings calls the database in its constructor to load the settings into a class. This seems odd to me because, the settings shouldn't change in the middle of running the app. So, how do you have your Application Settings called?? Do you use a static class or a singleton pattern instead of hitting the database each time to call the same settings??

+5  A: 

Yes, I usually throw that kind of stuff in a static Application class, especially if there's no reason to query multiple times.

Jarrett Meyer
+1  A: 

I'll go with the Singleton, which is really just a more controlled modification of Jarrett's answer. Go with which one fits your design.

And ignore all the bad press about Singleton's. Absolutely any construct can be abused, just use it judiciously.

HTH

JustBoo
There are a lot of reasons why singleton has gotten bad press and that a lot of good ones. One of Them being testability. Singltons are in most cases a design fault. Why would you want to depend ón an concrete implementation? Why would you wish to do procedural coding?
Rune FS
I could not disagree more. The herd has a particular smell.
JustBoo
+1  A: 

As you said, it's common to put that kind of responsibility on a singleton class that holds that application data.

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

You can modify the setting page automaticaly (from the DB) when your application starts and then read the values from anywhere in your application. See this post for more information about the ConfigurationManager Class http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

Good luck!

Adibe7
This would be for reading values from app.config at start-up. I'm not sure how it helps with configuration that's in the database.
Steven Sudit
You can modify it automaticaly (from the DB) when your application starts and then read the values from anywhere in your application.
Adibe7
Ok, and how do you know when they've been changed? In many cases, the expected behavior is not to care: just use what was valid when you started.
Steven Sudit
1. This what he originaly wrote : "This seems odd to me because, the settings shouldn't change in the middle of running the app".So i thought no changes will take place. 2. There are two solutions for what you asked:a. You can make a periodic check to the DB.b. The app could register to an event that the module that changed the DB (changed the configuration) will provide. (Can use the Observer pattern).
Adibe7
+2  A: 

Your classes are dependent on settings. So you might want to consider Dependency Injection to decouple them from how they are stored and improve the testability of your classes.

This is much easier if you use a dependency injection framework (Castle Windsor, NInject, etc.)

To avoid re-querying the database, you would create a Settings object that has Singleton lifetime. Avoid the static singleton like the plague. They make your application inherently untestable. See Static Singletons - The Anti-Pattern.

Rob
Could you clarify what you mean by a Settings object with a Singleton lifetime but not a static singleton?
Steven Sudit
I don't quite understand this. How can classes not depend on other classes unless you put all the data in one class? In this case, I think they're supposed to depend on the settings the user picks.
@Steven: The problem isn't with singleton, it's with the static implementation. So what you want to do is to create a class, but ensure that only one is ever created. Dependency Injection frameworks enforce this. You create your one class, give it singleton lifetime and add it to the DI container. Any class that needs the singleton asks the container for a copy. The container always passes back the one and only copy.
Rob
@Rob, I agree with you 100% on this. These settings are a dependency, and everyone else's answer hides that dependency. Also, I currently have several log files in my inbox because just this past week a static singleton class for Settings in an app I maintain was throwing transient exceptions during the static class constructor. Using a DI Framework to manage a Setting's class lifetime and being explicit with the classes that depend on it is absolutely the way to go.
qstarin
@user: It's not that the classes aren't dependent, it's that it's difficult to get in-between them and the dependency. For example, let's say you want to test a class under two variations of settings. There are ways to intercept the static calls to the singleton with mocking, but it's much easier to just pass in the two variations of settings from a unit test harness.
Rob
@qstarin: I'm currently unwinding a situation exactly like you describe. I think a lot of development teams have experienced the problems that static singletons create and how difficult it can be to refactor them out.
Rob
I'm still having a difficult time grasping this concept. Do you have an example?
@Rob: With a settings singleton, I'm not so much worried about a second copy being created, as it would be identical to the first. I'd just rather reuse the first rather than pay the cost of generating that duplicate. I don't see how letting the DI framework manage this is better than just making it static.
Steven Sudit
@user: I did a quick search and found this link: http://weblogs.asp.net/psteele/archive/2009/11/23/use-dependency-injection-to-simplify-application-settings.aspx for a configuration specific example. For a good general introduction to DI, see http://martinfowler.com/articles/injection.html
Rob
@Steven: It's subtle and hard to convey in a short comment, but I'll take a crack at it. DI is a better approach because it decouples each class from the "global" name of the static class. This has the same benefits as eliminating global variables. It's easier to use the class in a standalone manner, easier to test, easier to refactor. A DI container allows you to separate the concern of "lifetime" from your system. You could have configuration settings that are specific to a web request for example. The Fowler reference above is worth reading.
Rob
@Rob: I'm not actually new to DI, though I'm more likely to use it piecemeal on an ad hoc basis than wholesale through a proper framework. In the Fowler article, there is a relevant example fairly early on, with `ColonMovieFinder` having its initialization string configured an XML file. I would typically deal with this as a .NET Properties.Settings.Default singleton with a named property that is loaded from an XML config file. The difference, as far as I can see, is that the Spring example has that value directly associated with the class that uses it, as opposed to being available (cntd)
Steven Sudit
in what is essentially a global. Ok, I can see a (fairly small) advantage here, but I typically have config values that are of use to more than one class. Of course, you might be able to credibly argue that this is itself a design flaw, and that the "secret" should be in a single class that doles it out to others. Perhaps, but wouldn't that be yet another singleton? In the end, I find this interesting, and I'm not writing it off, but I have the feeling that I'm still missing something. Do you think you can point me at anything that can fill this blank in for me?
Steven Sudit
@Steven: Check out *xUnit Test Patterns: Refactoring Test Code* by Meszaros. One of the best software engineering books I've read.
Rob
Thanks, although something available online would be more convenient.
Steven Sudit