Configuration Section Value Collections

Application configuration, in .NET, relies on XML based files like App.config and Webconfig. Certain configuration sections support a collection of items, such as the connectionstrings section.

These collection based configurations support adding, removing and clearing the entire collection from inside the XML configuration file and rely on custom classes like NameValueConfigurationCollection or KeyValueConfigurationCollection. One can reuse these classes to support key/value or name/value collections inside custom configuration sections.

However if you only need simple value collections like strings or integers there isn’t a .NET class that you can reuse. A possible approach to fulfill this requirement follows:

public class ValueConfigurationElement<T> : ConfigurationElement
{
    private const string ValueAttributeName = "value";

    [ConfigurationProperty(ValueAttributeName, IsKey = true)]
    public T Value
    {
        get { return (T)this[ValueAttributeName]; }
        set { this[ValueAttributeName] = value; }
    }
}

public class ValueConfigurationCollection<T> : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new ValueConfigurationElement<T>();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ValueConfigurationElement<T>)element).Value.GetHashCode();
    }
}

These classes can then be used in a custom configuration section.

class CustomSection : ConfigurationSection
{
    private const string NamesElement = "names";
    private const string IntegersElement = "integers";

    [ConfigurationProperty(NamesElement)]
    public ValueConfigurationCollection<string> Names
    {
        get
        {
            return (ValueConfigurationCollection)base[NamesElement]
        }
    }

    [ConfigurationProperty(IntegersElement)]
    public ValueConfigurationCollection<int> Integers
    {
        get
        {
            return (ValueConfigurationCollection)base[IntegersElement]
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        var section = (CustomSection)ConfigurationManager.GetSection(
            "customSection");

        foreach (ValueConfigurationElement item in section.Names)
        {
            Console.WriteLine(item.Value);
        }

        foreach (ValueConfigurationElement item in section.Integers)
        {
            Console.WriteLine(item.Value);
        }
    }
}

And to end a possible definition of the custom section in the application configuration file.

<customSection>
  <names>
    <add value="John" />
    <add value="Jane" />
    <clear />
    <add value="Doe" />
    <remove value="Jane" />
  </names>
  <integers>
    <add value="5" />
    <add value="10" />
    <remove value="5" />
  </integers>
</customSection>
Advertisements

3 thoughts on “Configuration Section Value Collections”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s