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>
The key points for creation of your configuration class are: