-
Notifications
You must be signed in to change notification settings - Fork 0
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 ConfigurationAssist to extract those settings.
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 back end 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;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 implementation other than you type your section to the System.Configuration.DictionarySectionHandler, and you will use the DictionarySectionHandlerExtractionStrategy to extract the information.
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.
Note 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 any additional 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 however create a SingleTagSectionHandlerExtractionStrategy though. If you inspect the code however you will see that it just wraps the DictionarySectionHanderExtractionStrategy.
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;This is where we create our configuration inherited from ConfigurationSettings, and tightly tie the sections type to our class type and assembly. For an example on this please view our wiki page Sections - Simple
Though this is not a 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