Basic Implemention of an AppConfiguration Subclass

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 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
{
    public ApplicationConfiguration()
    {
           ApplicationTitle = "Sample Application";
           DebugMode = DebugModes.ApplicationErrorMessage;
           MaxPageItems = 20;
    }

    // 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
}

To use this class use code like this:

// Create an instance - typically you'd use a static singleton instance
var config = new ApplicationConfiguration();
config.Initialize();

// 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 the call to Initialize() which is necessary to initialize the class with a provider and do an initial read the configuration data. Here I'm using the default provider by not passing anything, which is the config file provider that will write into the application (or web) config file in a section that matches the class. Here an ApplicationConfiguration section will be created that 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>

The section in the config file is automatically created (assuming your app has rights to write the file), and any missing values are automatically created when re-reading the file.

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

Configuration Instance Management

Everytime you instantiate the Configuration class it reads configuration data from the store, and it's not terribly efficient to keep re-reading the data constantly. For this reason it's important to store the configuration instance in a static variable and re-use it easily in your application.

For more info see:

See also

Class AppConfiguration | Creating an Application wide, static Configuration Instance | Configuration with IConfigurationProviders

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