Click here to Skip to main content
16,020,622 members
Articles / DevOps
Tip/Trick

appSettings Gone Wild

Rate me:
Please Sign up or sign in to vote.
4.88/5 (7 votes)
18 Jan 2017CPOL2 min read 17K   8   5
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.

XML
<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.

XML
<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.

XML
<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:

XML
<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.

C#
//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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Steven Contos

Working in varied settings from small entrepreneurial companies to Fortune 500 companies. Skilled in analyzing client needs and developing solutions that are sound and effective.

Strong analytic capabilities with proven accomplishments in developing programs that exceed or meet stated goals, consistently work well, are easily maintained and fully documented. Versed in a number of SDLC technologies including Agile and Scrum, dedicated to deliver high quality software on time and on budget.

Experienced in helping companies and teams change their culture. Providing clear vision, asking tough questions of both developers and business, leading by example and building trust among all concerned.

Comments and Discussions

 
QuestionWhat dotnet version you used to code Pin
Tridip Bhattacharjee19-Jan-17 21:10
professionalTridip Bhattacharjee19-Jan-17 21:10 
AnswerRe: What dotnet version you used to code Pin
DotNetSteve20-Jan-17 6:47
DotNetSteve20-Jan-17 6:47 
GeneralMy vote of 5 Pin
dmjm-h19-Jan-17 10:34
dmjm-h19-Jan-17 10:34 
GeneralMy vote of 5 Pin
Jim_Snyder19-Jan-17 8:21
professionalJim_Snyder19-Jan-17 8:21 
QuestionThis is important... and here's somehting that might help Pin
Michael Breeden19-Jan-17 7:03
Michael Breeden19-Jan-17 7:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.