Saturday, July 25, 2015

Cascading AppSettings from Multiple Config Files

In my previous blog post I talked about creating complex configuration objects from AppSettings. I really like this practice, but it can cause your config files to grow pretty large. One solution is to break your app.config into multiple files using the SectionInformation.ConfigSource property.

This also has the added side effect of allowing you to include defaults and overrides by having those settings cascade. I have created an extension method to combine a series of NameValueCollections, such as an AppSettings section. You can also grab this code from the GitHub project.

Combine Extension

public static NameValueCollection Combine(
    this NameValueCollection collection, 
    params NameValueCollection[] collections)
    var result = new NameValueCollection { collection };
    foreach (var subCollection in collections)
        foreach (var key in subCollection.AllKeys)
            if (result.AllKeys.Contains(key))
            var value = subCollection[key];
            result.Add(key, value);
    return result;

Sample App.config

<?xml version="1.0" encoding="utf-8" ?>
    <section name="primaryValues" type="System.Configuration.NameValueFileSectionHandler, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <section name="defaultValues" type="System.Configuration.NameValueFileSectionHandler, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <add key="TestConfig.String" value="Moon"/>
  <defaultValues configSource="Default.config" />

Sample Default.config

<?xml version="1.0" encoding="utf-8" ?>
  <add key="TestConfig.Int" value="42"/>
  <add key="TestConfig.String" value="Goodnight"/>

Unit Test

public void CombineAndCreateObject()
    var primaryValues = (NameValueCollection) ConfigurationManager
    var primaryObject = primaryValues.CreateObject<TestConfig>();
    Assert.Equal("Moon", primaryObject.String);
    Assert.Equal(0, primaryObject.Int);
    var defaultValues = (NameValueCollection)ConfigurationManager
    var defaultObject = defaultValues.CreateObject<TestConfig>();
    Assert.Equal("Goodnight", defaultObject.String);
    Assert.Equal(42, defaultObject.Int);
    var combineValues = primaryValues.Combine(defaultValues);
    var combineObject = combineValues.CreateObject<TestConfig>();
    Assert.Equal("Moon", combineObject.String);
    Assert.Equal(42, combineObject.Int);


No comments:

Post a Comment

Real Time Web Analytics