ASP.NET 2.0 Configuration Manager Using Generics

 It is common to create new configuration settings in your web.config file. The recommended practice for accessing custom configuration settings proposes that you then read the values into a NameValueCollection. Taking this practice one step further, you can, with the aid of generics, implement a configuration manager class that is able to convert your value into its expected datatype. For this sample, I was inspired by the implementation of an ASP.Net 2.0, XML-based Membership provider after viewing the Exploring the New Personalization Features in ASP.NET 2.0 (Level 200) webcast. The driving vision for this was to make an extremely easy and thoughtless implementation. See the useage section below; specifially the accessing of the values in the public properties.
 


 

using

System;

using System.Collections.Generic;

using

System.Text;

using

System.Configuration;

 

namespace

CustomProviders {

          public abstract class ConfigurationManagerBase {

                    public const string CONFIGURATION_IS_NULL = "Configuration is null.";

                    public const string CONFIGURATION_PROPERTY_NOT_FOUND = "Configuration property not found.";

                    private readonly System.Collections.Specialized.NameValueCollection config;

                    public ConfigurationManagerBase(System.Collections.Specialized.NameValueCollection config) {

                              if (config == null) {

                                        throw new ConfigurationException(CONFIGURATION_IS_NULL);

                              }

                              this.config = config;

                    }

                    protected T GetValue<T>(string key) {

                              string propertyValue = ConfigurationPropertyValue(config, key);

                              return ConvertToExpectedType<T>(propertyValue);

                    }

                    private string ConfigurationPropertyValue(System.Collections.Specialized.NameValueCollection config, string key) {

                              if (((IList<string>)config.AllKeys).Contains(key)) {

                                        return config[key];

                              } else {

                                        throw new ConfigurationException(CONFIGURATION_PROPERTY_NOT_FOUND);

                              }

                    }

                    private T ConvertToExpectedType<T>(string propertyValue) {

                              T value = default(T);

                              try {

                                        value = (T)Convert.ChangeType(propertyValue, typeof(T));

                                        return value;

                              } catch {

                                        throw new InvalidCastException(String.Format("Could not convert {0} to {1}", propertyValue, typeof(T).ToString()));

                              }

                    }

          }

          public enum XmlMembershipConfigurationProperty {

                    ApplicationName,

                    MinRequiredNonAlphanumericCharacters

          }

          public class XmlMembershipConfigurationManager : ConfigurationManagerBase {

          public XmlMembershipConfigurationManager(System.Collections.Specialized.NameValueCollection config) : base(config) { }

                    public T GetValue<T>(XmlMembershipConfigurationProperty property) {

                              string camelCaseProperty = SetCamelCase(property.ToString());

                              return base.GetValue<T>(camelCaseProperty);

                    }

                    private string SetCamelCase(string propVal) {

                              string camelCaseProperty = propVal.Substring(0, 1).ToString().ToLower() + propVal.Substring(1, propVal.Length – 1);

                              return camelCaseProperty;

                    }

          }

}

 

Useage:

 

public class SomeClass {

          private System.Collections.Specialized.NameValueCollection config;

          private XmlMembershipConfigurationManager configManager;

          private static string KEY_0 = "applicationName";

          private const string VALUE_0 = "JasonHunt";

          private static string KEY_1 = "minRequiredNonAlphanumericCharacters";

          private const string VALUE_1 = "2";

          public SomeClass() {

                    /* Of course you would normally retrieve

                    * the configuration settings as prescribed.

                    * This is used to make the point of how to

                    * use this class. */

                    config = new System.Collections.Specialized.NameValueCollection();

                    config.Add(KEY_0, VALUE_0);

                    config.Add(KEY_1, VALUE_1);

                    configManager = new XmlMembershipConfigurationManager(config);

          }

          public string ApplicationName {

                    get { return configManager.GetValue<string>(XmlMembershipConfigurationProperty.ApplicationName); }

                    }

          public int MinRequiredNonAlphanumericCharacters {

                    get { return configManager.GetValue<int>(XmlMembershipConfigurationProperty.MinRequiredNonAlphanumericCharacters); }

          }

}

 


References:

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s