This mechanism uses a class-first approach to configuration management where you declare your configuration settings as simple class properties on a subclass of this abstract class. The class then automatically reads configuration information from the configuration store into the class' properties. If settings don't exist they are auto-created, permissions permitting. You can create multiple configuration objects. The advantage of this class centric approach is that your configuration is not bound to any particular environment - you can use the configuration in a Web, desktop, console or service app using the same mechanism.
Using this class is easy: You create a subclass of AppConfiguration add a constructor (or two) and then simply add properties to the class. The implementation will handle persistence transparently. Typical implementation defines the configuration class as a static property on a global object of some sort (ie. App.Configuration.MySetting).
Storage is configured via a configuration provider that configures provider specific features. The configuration class then uses the provider to read and write data from the configuration store. The base providers also support encryption of individual fields.
public abstract class AppConfiguration : object
/// <summary>
/// Your application specific config class
/// </summary>
public class ApplicationConfiguration :
Westwind.Utilities.Configuration.AppConfiguration
{
public ApplicationConfiguration()
{ }public ApplicationConfiguration(IConfigurationProvider provider)
: base(provider, ""ApplicationConfiguration")
{
if (provider == null)
{
Provider = newConfigationFileConfigurationProvider<ApplicationConfiguration>()
MailServerPassword,ConnectionString",
{
PropertiesToEncrypt =
"
EncryptionKey = "secret",
ConfigurationSection = "ApplicationConfiguration"
};// Example of Sql configuration
MailServerPassword,ConnectionString",
//Provider = new
//
SqlServerConfigurationProvider<ApplicationConfiguration>()
//{
// FieldsToEncrypt =
"
// EncryptKey = "secret",
// ConnectionString = "DevSampleConnectionString",
// Tablename = "Configuration",
// Key = 1
//};
// Example of external XML configuration
//Provider = new
XmlFileConfigurationProvider<ApplicationConfiguration>()
//{
// FieldsToEncrypt =
"MailServerPassword,ConnectionString",
// EncryptKey = "secret",
// XmlConfigurationFile =
//
HttpContext.Current.Server.MapPath("~/Configuration.xml")
//};
}
else
Provider = provider;Provider.Read(this);
}// persisted configuration properties - note type suppport
West Wind Web Toolkit";
public string ApplicationTitle
{
get { return _ApplicationTitle; }
set { _ApplicationTitle = value; }
}
private string _ApplicationTitle = "public string ConnectionString
";
{
get { return _ConnectionString; }
set { _ConnectionString = value; }
}
private string _ConnectionString = "public DebugModes DebugMode
{
get { return _DebugMode; }
set { _DebugMode = value; }
}
private DebugModes _DebugMode =
DebugModes.ApplicationErrorMessage;public int MaxPageItems
{
get { return _MaxPageItems; }
set { _MaxPageItems = value; }
}
private int _MaxPageItems = 20;
}
Class Hookup as a static Property
public class App
{
public static ApplicationConfiguration Configuration
{
get { return _Configuration; }
set { _Configuration = value; }
}
private static ApplicationConfiguration _Configuration;static App()
{
/// Load the general Application config properties from the Config
file
Configuration = new ApplicationConfiguration(null);}
}
Usage from anywhere in the Application
var title =
App.Configuration.ApplicationTitle;
if (App.Configuration.DebugMode == DebugModes.Default)
throw new ApplicationException("Boo");
Member | Description | |
---|---|---|
Constructor | Default constructor of this class SHOULD ALWAYS be implemented in every subclass to allow serialization instantiation. Should be left empty. | |
ErrorMessage | Contains an error message if a method returns false or the object fails to load the configuration data. | |
Provider | An instance of a IConfigurationProvider that needs to be passed in via constructor or set explicitly to read and write from the configuration store. | |
Read | Reads the configuration from the provider's store into the current object instance. public virtual bool Read();
public virtual bool Read(string xml);
|
|
Read<T> | Reads the configuration information from the provider's store and returns a new instance of an configuration object. public virtual T Read<T>();
public static T Read<T>(IConfigurationProvider provider);
public static T Read<T>(string xml,
IConfigurationProvider provider); public static T Read<T>(string xml);
|
|
Write | Writes the current configuration information data to the provider's configuration store. public virtual bool Write();
|
|
WriteAsString | Writes the current configuration information to an XML string. String is in .NET XML Serialization format. public virtual string WriteAsString();
|