Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

appSettings Gone Wild

0.00/5 (No votes)
18 Jan 2017 1  
Manage an overgrown appSettings section

Introduction

I am sure we have all seen an appSettings section of an application or web configuration which has grown way too large. As different features are added to an application, values which are stored at an application level are added to the config file.

Over time, this section can grow large and unwieldy with different parts of the section pertaining to different parts of the features in the application.

Background

While working on many projects, over the years, I have seen a couple of options for organizing and grouping similar items.

The first code snippet shows the fact that keys need to be named uniquely.

<appSettings>
  <add key="connection1" value="somevalue"/>
  <add key="username1" value="somevalue"/>
  <add key="password1" value="somevalue"/>
  <add key="connection2" value="somevalue"/>
  <add key="username2" value="somevalue"/>
  <add key="password2" value="somevalue"/>
  <add key="connection3" value="somevalue"/>
  <add key="username3" value="somevalue"/>
  <add key="password3" value="somevalue"/>
</appSettings>

Of course, the problem with something like this is that the keys are strings and as a developer, you need to know what the key string is used for (connection1 is for EWS).

The second method I have experienced was grouping keys using namespaces.

<appSettings>
  <add key="ews.connection" value="somevalue"/>
  <add key="ews.username" value="somevalue"/>
  <add key="ews.password" value="somevalue"/>
  <add key="fax.connection" value="somevalue"/>
  <add key="fax.username" value="somevalue"/>
  <add key="fax.password" value="somevalue"/>
  <add key="device.connection" value="somevalue"/>
  <add key="device.username" value="somevalue"/>
  <add key="device.password" value="somevalue"/>
</appSettings>

Using the namespace "ews.", you have scoped the setting to the ews feature.

Of course, this still does not solve the issue with the size of the appSettings and just stuffing things into it.

The Change

One of the best features of the XML format used by the config file is the ability to add new sections as needed. That's why you will typically see sections added for logging and entity framework.

<configSections>
  <section name="entityFramework" 
      type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, 
            EntityFramework, 
            Version=6.0.0.0, 
            Culture=neutral, 
            PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  <section name="log4net" 
      type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<entityFramework>
   <!-- entries for entity framework -->   
</entityFramework>
<log4net>
   <!-- entries for log4net -->
</log4net>

For each of these newly added config sections, you will find corresponding XML fragments named as name (see above). Each section is defined by the author of the component and the elements are defined by them.

Adding Your Own Section

Microsoft has two pre-built configuration sections which allow for adding a new named section with keys and values: System.Configuration.NameValueSectionHandler and System.Configuration.DictionarySectionHandler.

Both can be used in the same way:

<configSections>   
 <section name="entityFramework"
   type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
   EntityFramework,
   Version=6.0.0.0,
   Culture=neutral,
   PublicKeyToken=b77a5c561934e089" requirePermission="false" />   
 <section name="log4net"
   type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
 <section name="EWSInfo" 
 type="System.Configuration.NameValueSectionHandler" />
 <section name="Fax" 
 type="System.Configuration.NameValueSectionHandler" />
</configSections>
  <EWSInfo>
    <add key="connection" value="somevalue"/>
    <add key="username" value="somevalue"/>
    <add key="password" value="somevalue"/>
  </EWSInfo>
  <Fax>
    <add key="connection" value="somevalue"/>
    <add key="username" value="somevalue"/>
    <add key="password" value="somevalue"/>
  </Fax>

The code to read these sections is a little different. Normally, you would use the ConfigurationManager.AppSettings method to read from the appSettings section. Now, you use GetSection to get the values of the named section.

//You start with getting the new section as the type specified
var EWSInfo = (NameValueCollection)ConfigurationManager.GetSection("EWSInfo");

//You can check to see if the section exists, if not EWSInfo will be null
//Use EWSInfo the same way you would use appSettings

string connection = EWSInfo["connection"];
string username = EWSInfo["username"];
string password = EWSInfo["password"];

In my code, I actually used the test for the section to turn options on and off in the application.

For example, if the Fax section is missing, I won't give you an option to fax.

Hope you find this helpful and if you have comments or questions, please feel free to ask.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here