Skip to content

Section Types and Strategies Explained

Jeremy van Dyk edited this page Nov 22, 2015 · 13 revisions

Section Types and Strategies Explained

When working with configuration sections .Net offers multiple ways of setting the underlying type. I'm going to explain the main implementations and how to use ConfigAssist to extract those settings.

1. NameValueSectionHandler

One of the more common types is the NameValueSectionHandler. This simply lets us work with name value pairs similar to how AppSettings works. In the code backend this binds to a NameValueCollection object. Here is an example of how to work with this type of section using ConfigAssist.

Configuration below, note the type specified, and the fact that we don't specify the assembly. We can if we want, but its not necessary most of the time. Note however that the assembly is System.

<configuration>
  <configSections>
    <section name="ValueKeySectionConfiguration" type="System.Configuration.NameValueSectionHandler"/>
  </configSections>

  <ValueKeySectionConfiguration>
    <add key="Name" value="MyConfigSection" />
    <add key="MaxValue" value="1000000000" />
  </ValueKeySectionConfiguration>
</configuration>

Next we layout our Configuration Class. I'm going to do this in the simplest format by laying out the name and parameter names (case) identical to the configuration. Using NameValueSectionHandler we don't need to inherit our class from ConfigurationSection

public class ValueKeySectionConfiguration
{
  public string Name { get; set; }
  public long MaxValue { get; set; }
}

Nice and simple. Next we implement our extraction strategy to retrieve the settings.

var configAssist = new ConfigurationAssist();
var configuration = configAssist.ExtractSettings<ValueKeySectionConfiguration>(new NameValueHandlerSectionExtractionStrategy());

//you can now read the values anyway you want. I'm just going to show they are typed.
string name = configuration.Name;
long maxValue = configuration.MaxValue;

2. DictionarySectionHandler

The dictionary section handler is almost identical to the NameValueSectionHandler, except the underlying object is a HashTable. This means much better performance when there are a large number of keys. As such, I'm not going to explain the implmentation other than you type your section to the System.Configuration.DictionarySectionHandler, and you will use the DictionarySectionHandlerExtractionStrategy to extract the information.

3. SingleTagSectionHandler

This section handler is similar to the custom strongly named type from our (sections - simple) example, except you still don't need to have your config inherit from the ConfigurationSection class. Not this type allows you to use xml attributes for your property values. Below is a full example:

<configuration>
  <configSections>
    <section name="SingleTagSectionConfiguration" type="System.Configuration.SingleTagSectionHandler"/>
  </configSections>

  <SingleTagSectionConfiguration
    Name="MyConfigSection"
    MaxValue="1000000000" />
</configuration>

Next we create our config. Once again I'm going to name the config and its properties exactly the same as in the configuration file so we don't need to perform mapping. We also don't need to inherit from ConfigurationSettings for this type.

public class SingleTagSectionConfiguration
{
  public string Name { get; set; }
  public long MaxValue { get; set; }
}

Finally we use an extraction strategy to extract our settings. In this case we will use SingleTagSectionHandlerExtractionStrategy. Its interesting to note that the SingleTag actually binds to a HashTable object. So you could also use the DictionarySectionHanderExtractionStrategy if you want. We did create the SingleTag one though. If you inspect the code however you will see that it just wraps the dictionary strategy.

var configAssist = new ConfigurationAssist();
var configuration = configAssist.ExtractSettings<SingleTagSectionConfiguration>(new SingleTagSectionHandlerExtractionStrategy());

//you can now read the values anyway you want. I'm just going to show they are typed.
string name = configuration.Name;
long maxValue = configuration.MaxValue;

4. Custom Types

This is where we create our configuration inherited from ConfigurationSettings, and tightly tie the type to our type and assembly. For an example on this please view our wiki page Sections - Simple

5. App Settings

Though this is not section in the same way the others are, this type uses the AppSettingsExtractionStrategy to extract the settings. An example can be found on the wiki page AppSettings

Clone this wiki locally