The AppConfiguration class is a base class that provides core configuration persistance functionality to your application. To create your own custom configuration class the concept is very simple: You create a class that inherits from AppConfiguration and add a constructor that determines the default load behavior, and then add properties that become your configuration values.

Creating a default Application Configuration Class

In its simplest form you can create your configuration class like this:

public class ApplicationConfiguration : Westwind.Utilities.Configuration.AppConfiguration { // web.config or app.config and specify a section to write to public ApplicationConfiguration() : base(null,"ApplicationConfiguration") { } // Create properties for values to read or persist to/from the config store public string ApplicationTitle { get; set; } public string ConnectionString {get; set; } public DebugModes DebugMode {get; set; } // enum public int MaxPageItems {get; set; } // number // put property initialization into this separate method public override void Initialize() { // set any default values or other init functionality ApplicationTitle = "Sample Application"; DebugMode = DebugModes.ApplicationErrorMessage; MaxPageItems = 20; } }

To use this class use code like this:

// Create an instance - typically you'd use a static singleton instance var config = new ApplicationConfiguration(); // Read values - retrieved from web.config/MyApplicationConfiguration string title = config.ApplicationTitle; DebugModes modes = config.DebugMode; // You can also update values config.MaxPageItems = 15; // And save changes to config store if permissions allow config.Write();

Note that the values are automatically turned into the appropriate strong types. The DebugModes Enum comes back as an enum as does the MaxPageItems integer. Basic type conversion from string to the simple types is fully automatic so you don't have to manually convert.

Unlike this example, it's recommended that you create your configuration object(s) only once per application during startup and then reference them off a static property like App.Configuration.MaxPageItems. This avoids having to re-read the configuration information from the store repeatedly.

A better Choice: Using Global Configuration Objects
The following example creates a 'global' static App object that has a Configuration property:

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(); } }

Once that's in place you can access the static configuration member from anywhere in your application with:

int pageItems = App.Configuration.MaxPageItems; DebugModes mode = App.Configuration.DebugMode;

The configuration data in this case is stored in the application's .config file. AppConfiguration handles the creation of the section header and when first loaded will automatically write out the configuration data to the store (if permissions are available to do so). The data written looks like this:

<ApplicationConfiguration> <add key="ApplicationTitle" value="West Wind Web Toolkit Sample" /> <add key="ConnectionString" value="DevSamplesConnectionString" /> <add key="DebugMode" value="ApplicationErrorMessage" /> <add key="MaxPageItems" value="20" /> </ApplicationConfiguration>

AppConfiguration SubClass Key Points

The key points for creation of your configuration class are:

  • A constructor that determines the default configuration store
    The constructor(s) configure the store for your configuration class. Here the call to base(null,"MyAppConfiguration") provides a default implementation that accesses web.config (app.config) and writes values to the MyApplicationConfiguration section. The first parameter is an IConfigurationProvider interface which allows providers for different stores and full customization of the behavior of your configuration class (more on this later).

  • Properties for each of the configuration values you want to store
    Each configuration value you want to store is represented by a strongly typed property. AppConfiguration automatically handles the type conversion of simple types from the string values used in the configuration store. Need a new configuration value: Simple add a property! When the class is instantiated it reads the values from the file into the configuration instance. If you have write access to the configuration store it also writes the values out to the store if a key or keys don't exist and you can explicitly write a configured object to the store.