<p>A database application that I'm currently working on, stores all sorts of settings in the database. Most of those settings are there to customize certain business rules, but there's also some other stuff in there.</p>

<p>The app contains objects that specifically do a certain task, e.g., a certain complicated calculation. Those non-UI objects are unit-tested, but also need access to lots of those global settings. The way we've implemented this right now, is by giving the objects properties that are filled by the Application Controller at runtime. When testing, we create the objects in the test and fill in values for testing (not from the database).</p>

<p>This works better, in any case much better than having all those objects need some global <em>Settings</em> object --- that of course effectively makes unit testing impossible :) Disadvantage can be that you sometimes need to set a dozen of properties, or that you need to let those properties 'percolate' into sub-objects.</p>

<p><strong>So the general question is</strong>: how do you provide access to global application settings in your projects, without the need for global variables, while still being able to unit test your code? This must be a problem that's been solved 100's of times...</p>

<p>(Note: I'm not too much of an experienced programmer, as you'll have noticed; but I love to learn! And of course, I've already done research into this topic, but I'm really looking for some first-hand experiences)</p>

[{'answer_id': 12161, 'author': 'JamesSugrue', 'author_id': 1075, 'author_profile': 'https://Stackoverflow.com/users/1075', 'pm_score': 0, 'selected': False, 'text': "<p>Usually this is handled by an ini file or XML configuration file. Then you just have a class that reads the setting when neeed.</p>\n\n<p>.NET has this built in with the ConfigurationManager classes, but it's quite easy to implement, just read text files, or load XML into DOM or parse them by hand in code.</p>\n\n<p>Having config files in the database is ok, but it does tie you to the database, and creates an extra dependancy for your app that ini/xml files solve.</p>\n"}, {'answer_id': 12183, 'author': 'Lars Mæhlum', 'author_id': 960, 'author_profile': 'https://Stackoverflow.com/users/960', 'pm_score': 0, 'selected': False, 'text': '<p>I did this:</p>\n\n<pre><code>public class MySettings\n{\n    public static double Setting1\n        { get { return SettingsCache.Instance.GetDouble("Setting1"); } }\n\n    public static string Setting2\n        { get { return SettingsCache.Instance.GetString("Setting2"); } }\n}\n</code></pre>\n\n<p>I put this in a separate infrastructure module to remove any issues with circular dependencies.<br>\nDoing this I am not tied to any specific configuration method, and have no strings running havoc in my applications code.</p>\n'}, {'answer_id': 12185, 'author': 'Magnar', 'author_id': 1123, 'author_profile': 'https://Stackoverflow.com/users/1123', 'pm_score': 2, 'selected': True, 'text': "<p>You could use Martin Fowlers ServiceLocator pattern. In php it could look like this:</p>\n\n<pre><code>class ServiceLocator {\n  private static $soleInstance;\n  private $globalSettings;\n\n  public static function load($locator) {\n    self::$soleInstance = $locator;\n  }\n\n  public static function globalSettings() {\n    if (!isset(self::$soleInstance-&gt;globalSettings)) {\n      self::$soleInstance-&gt;setGlobalSettings(new GlobalSettings());\n    }\n    return self::$soleInstance-&gt;globalSettings;\n  }\n}\n</code></pre>\n\n<p>Your production code then initializes the service locator like this:</p>\n\n<pre><code>ServiceLocator::load(new ServiceLocator());\n</code></pre>\n\n<p>In your test-code, you insert your mock-settings like this:</p>\n\n<pre><code>ServiceLocator s = new ServiceLocator();\ns-&gt;setGlobalSettings(new MockGlobalSettings());\nServiceLocator::load(s);\n</code></pre>\n\n<p>It's a repository for singletons that can be exchanged for testing purposes.</p>\n"}, {'answer_id': 12210, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 1, 'selected': False, 'text': '<p>I like to model my configuration access off of the Service Locator pattern.  This gives me a single point to get any configuration value that I need and by putting it outside the application in a separate library, it allows reuse and testability.  Here is some sample code, I am not sure what language you are using, but I wrote it in C#.</p>\n\n<p>First I create a generic class that will models my ConfigurationItem.</p>\n\n<pre><code>public class ConfigurationItem&lt;T&gt;\n{\n    private T item;\n\n    public ConfigurationItem(T item)\n    {\n        this.item = item;\n    }\n\n    public T GetValue()\n    {\n        return item;\n    }\n}\n</code></pre>\n\n<p>Then I create a class that exposes public static readonly variables for the configuration item.  Here I am just reading the ConnectionStringSettings from a config file, which is just xml.  Of course for more items, you can read the values from any source.</p>\n\n<pre><code>public class ConfigurationItems\n{\n    public static ConfigurationItem&lt;ConnectionStringSettings&gt; ConnectionSettings = new ConfigurationItem&lt;ConnectionStringSettings&gt;(RetrieveConnectionString());\n\n    private static ConnectionStringSettings RetrieveConnectionString()\n    {\n        // In .Net, we store our connection string in the application/web config file.\n        // We can access those values through the ConfigurationManager class.\n        return ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["ConnectionKey"]];\n    }\n}\n</code></pre>\n\n<p>Then when I need a ConfigurationItem for use, I call it like this:</p>\n\n<pre><code>ConfigurationItems.ConnectionSettings.GetValue();\n</code></pre>\n\n<p>And it will return me a type safe value, which I can then cache or do whatever I want with.</p>\n\n<p>Here\'s a sample test:</p>\n\n<pre><code>[TestFixture]\npublic class ConfigurationItemsTest\n{\n    [Test]\n    public void ShouldBeAbleToAccessConnectionStringSettings()\n    {\n        ConnectionStringSettings item = ConfigurationItems.ConnectionSettings.GetValue();\n        Assert.IsNotNull(item);\n    }\n}\n</code></pre>\n\n<p>Hope this helps.</p>\n'}]
['https://Stackoverflow.com/questions/12140', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1037/']