Creating Custom Configuration Settings

The .NET framework has several options to offer when it comes to storing configuration settings in your configuration file.  When storing simple key-value settings, the AppSettings section may be all that you need.  However, in other cases, you might want to store more complex configuration settings.  If that is the case, creating your own, custom configuration section may be a better option.

.NET Framework 2.0 allows you to create a class that inherits from the ConfigurationSection class, which makes creating a custom configuration section a piece of cake.  The class I am showing here as an example is not very spectacular, but show the most important features.  The class represents settings that might be used for a form in your application that allows users to contact you.  This example assumes that all mail from your application will be sent to one, configurable email address.  It also assumes that you might want to prefix the subject (as entered by the user) with a short prefix, so you can use it for easy filtering in your mail client.

First, you need to create the class that will be used to retrieve the configuration settings from te configuration file:

namespace CustomConfigurationDemo
{
  public class MailFormConfiguration : ConfigurationSection
  {
    [ConfigurationProperty("mailTo",
      IsRequired = true)]
    public string MailTo
    {
      get
      {
        return (String)this["mailTo"];
      }
      set
      {
        this["mailTo"] = value;
      }
    }
    [ConfigurationProperty("subjectPrefix",
      DefaultValue = "",
      IsRequired = false)]
    public string SubjectPrefix
    {
      get
      {
        return (String)this["subjectPrefix"];
      }
      set
      {
        this["subjectPrefix"] = value;
      }
    }
  }
}

The important things to note here, are:

  • The class inherits from ConfigurationSection;
  • Each property has a ConfigurationProperty attribute

For the ConfigurationProperty attribute to work, you must specify the name of the attribute in the coniguration file that will be used to retrieve the value.  The ConfigurationProperty attribute also allows you to specify a DefaultValue, and you can use IsRequired to indicate whether the attribute must be included in the configuration file.  If you set IsRequired to true and the attribute is not found in the configuration file, a ConfigurationErrorsException will be thrown.

In order to use this MailFormConfiguration class, you will need to include the following in your configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="mailFormConfiguration"
             type="CustomConfigurationDemo.MailFormConfiguration,
                   CustomConfigurationDemo" />
  </configSections>
  <mailFormConfiguration mailTo="somebody@somepany.com" />
</configuration>

As you can see, the subjectPrefix attribute is not required, and is not included in the configuration file.  Its value will be set to the default value specified using the ConfigurationProperty attribute which, in this case, is an empty string.

The following code fragment shows how the custom configuration settings class can be used in your application:

MailFormConfiguration config =
  (MailFormConfiguration)ConfigurationManager.GetSection(
  "mailFormConfiguration");
string mailTo = config.MailTo;
string subjectPrefix = config.SubjectPrefix;

If you found this post helpful, please click below to “Kick” it:

kick it on DotNetKicks.com

About these ads

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


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: