RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Building a Better Configuration Settings Class

.NET provides a basic configuration management class, but it's not as flexible or easy to use as it could be. Find out how you can create a more flexible interface that provides strong typing, encryption, and write access to your application settings.

onfiguration settings make it possible for users and administrators to configure an application before it is run for the first time and while it runs. .NET provides a good rudimentary mechanism for storing and retrieving configuration settings in the application's .config file with the ConfigurationSettings class, but this mechanism is missing a number of essential features. This article describes how to improve on the base functionality using a class that provides strong typing, allows for writing of keys, and provides optional encryption of keys.

I consider configuration information a vital component of any application and use it extensively for allowing customization of the application both at runtime and through external configuration settings. I try to make as many options user-configurable as possible and configure everything from user interface elements, to top level business logic options, all the way to developer options that allow me to do things like switch in and out of detailed debug modes, turn on logging or tracing, and so on. A Configuration class is not exactly a sexy feature, but it's quite vital to the usability and configurability of an application.

It should be easy to create and maintain configuration information. If you have to write a lot of code or remember every setting, you'll end up not using configuration settings all that much, resulting in an application that isn't as configurable as it could be.

What .NET Provides
Natively, .NET provides a rudimentary mechanism through the ConfigurationSettings.AppSettings class. This class reads key values from a special section in the application's .config file. Here's a section in Web.config after some keys were added.

        <add key="ConnectionString" 
           enlist=false;" />
        <add key="ConnectType" value="SqlServer" />
        <add key="TaxRate" value="0.0400" />
        <add key="TaxState" value="HI" />
        <add key="ItemFormListPageCount" value="10" />
        <add key="SendAdminEmail" value="False" />

        ... more settings

All the information in the AppSettings section is stored in string format. You can retrieve this information easily by using the ConfigurationSettings.AppSettings object:

   string ConnectionString = 

It seems easy enough. And it is easy if:

  • The value you're retrieving is a string, and
  • The value exists in the configuration file.
The .NET ConfigurationSettings class, although easy to use, still requires a fair amount of code to access settings reliably.
If the key doesn't exist, the value is returned as null. So if you plan on using the value above, you need to make sure that you add a null check. If you look at the configuration example above, you'll also notice that there are several values that are non-string types: TaxRate is a decimal, SendAdminEmail is a Boolean, and ConnectType is actually an Enum value. The AppSettings object only returns strings and so you have to convert these strings back into numeric, Boolean, or enum type values. The code to do this safely within your code gets long quickly. Here's an example of picking up a decimal value and converting it.

   decimal TaxRate = 0.00;
   string cTaxRate = Configuration.AppSettings["TaxRate"];
   if (cTaxRate != null) 
         TaxRate = decimal.Parse(cTaxRate);
      catch {;}

If you have to write this much code each time you want to use a non-string value, it becomes unwieldy quickly. In my experience, if configuration settings aren't easy to access, you won't use them and you'll end up hard-coding stuff that should be configurable.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date