Creating an Application wide, static Configuration Instance

In order to use the configuration class effectively you should use a static instance of the class to avoid having to reload the configuration data from the store on every access. Since the class opens and reads the configuration store each time you create the instance and call Initialize(), this overhead should be avoided.

Static instances are a nice way to ensure the configuration data is only read once in the static constructor and provides easy global access to the configuration data from anywhere in your application.

We recommend you create an application level class that is used to hold static reference to any application global data, constants and objects. Your Configuration object should be one of the static properties. While you can also use any static object in the application framework, like Application object in ASP.NET we still recommend a separate object so the Application configuration and 'globals' object are portable with your business logic.

Here's a typical setup:

public class App
{
    // static property on any class
    public static ApplicationConfiguration Configuration { get; set; }

    // static constructor ensures this code runs only once 
    // the first time any static property is accessed
    static App()
    {
        /// Load the properties from the Config store
        Configuration = new ApplicationConfiguration();
        Configuration.Initialize();       
    }
}

The App class has a static property ApplicationConfiguration that holds our custom configuration class. A static constructor is used to load the property with the configuration object exactly once. Static constructors are guaranteed to only fire once per application and are fired on the first access of a static propery, which is great to avoid overhead of reloads and potential conflicts between different threads in multi-threaded or Web applications.

The first time you access the Configuration property the static constructor fires and the configuration object is loaded with settings from the configuration store when Initialize() is called. After that configuration data comes from the static/global instance only, unless you explicitly re-read the data from the store using Read() or Write() on the configuration object.

From then on you can access the configuration anywhere in your application via App.Configuration.Property. For example:

int maxListItems = App.Configuration.MaxPageItems;
this.Page.Title += " - " + App.Configuration.ApplicationTitle;

The beauty of the static property is that it's accessible from anywhere and you never need to worry about instantiation of the configuration class outside of the static construtor. You simply reference the App.Configuration object and it will always be available and the configuration values are loaded only once when the first configuration property is accessed.

The configuration class is globally available and accessible because it is static. When you need to update saved values back into the configuration store call the Write() method to persist the configuration:

App.Configuration.ApplicationTitle = "Something new is really needed";
App.Configuration.Write()

Note that writing to the store requires the appropriate permissions. For example in a Web application the Web Server account needs write permissions for the web.config file, and in a Windows app the active user might have to have User Account Control off in order to write to the configuration file. Permissions might be a good reason to use custom configuration store like a database or an explictly stored XML or config file in a user accessible folder in Windows' AppData for example.


© West Wind Technologies, 1996-2016 • Updated: 12/19/15
Comment or report problem with topic