-
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 retrieve the settings.
var configAssist = new ConfigurationAssist();
var configuration = configAssist.ExtractSettings<ValueKeySectionConfiguration>();
//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.
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; }
}Now we extract our settings
var configAssist = new ConfigurationAssist();
var configuration = configAssist.ExtractSettings<SingleTagSectionConfiguration>();
//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.
This Section is still under development and currently only caters for simple class types. We are adding the ability to use complex types, and the example will be shows when this section is ready.
Though this is not a section in the same way the others are the AppSettings can also be extracted. Note you can sub group your appsettings into multiple configuration classes. There is a simple example on the Basic Examples page.