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 string
s 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>
</entityFramework>
<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.
var EWSInfo = (NameValueCollection)ConfigurationManager.GetSection("EWSInfo");
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.