Cinchoo – Configuration framework, part 13

Managing Large Configuration file

Download sample source files (Require .NET 4.0 / Visual Studio 2010)

Suppose in a large application built on several components packaged in their own assemblies, managing configuration file in one file will be tedious. Using Cinchoo configuration framework, you can split them into manageable small files. I’ll walk you over the steps to split them and use them in a application.

There are couple of ways, you can achieve the splitting of configuration files

1. Automatic Split (Code first approach)

This approach is viable in case of new development where there are no existing configuration files available.  In this scenario, you just design and implement the configuration objects first and let the Cinchoo framework generate the configuration files for you. I’ll walk you over on how to do it using this approach.

You can specify the individual configuration file for each configuration object using ChoConfigurationSectionAttribute, where you must give the name of the configuration file in one of the property

  • ConfigFilePath – Configuration file path in string either full path or just file name.
  • ConfigFilePathFromTypeName – For a given type specified to this property, it takes the name of the type as configuration file name.
  • ConfigFilePathFromTypeFullName – For a given type specified to this property, it takes the fully qualified namespace name of the type as configuration file name.

For a sample console application named SplitConfigUsingCodeFirstApproach.exe, defining the below two configuration objects

[ChoNameValueConfigurationSection("serverDetails/environments", ConfigFilePathFromTypeName = typeof(ServerEnvironments))]
public class ServerEnvironments : ChoConfigurableObject
{
    public string Host = "WNYC1000234011";
    public string IP = "205.12.5.122";
    public int Port = 2001;
}

[ChoNameValueConfigurationSection("applicationSettings", ConfigFilePath = "ApplicationSettings" )]
public class ApplicationSettings : ChoConfigurableObject
{
    public string Mode = "LIVE";
    public bool IsConnected = true;
    public string Exchange = "NYSE";
}

Just instantiate the above objects in the Main entry method as below, Cinchoo framework creates the configuration files automatically.

class Program
{
    static void Main(string[] args)
    {
        ServerEnvironments serverEnvironments = new ServerEnvironments();
        ApplicationSettings applicationSettings = new ApplicationSettings();

        ChoFramework.Shutdown();
    }
}

The main application configuration file (SplitConfigUsingCodeFirstApproach.exe.xml)  is generated as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <applicationSettings cinchoo:path="C:\CinchooConfigurationFrxDemos\SplitConfigUsingCodeFirstApproach\SplitConfigUsingCodeFirstApproach\bin\Debug\ApplicationSettings.xml" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" />
  <serverDetails>
    <environments cinchoo:path="C:\CinchooConfigurationFrxDemos\SplitConfigUsingCodeFirstApproach\SplitConfigUsingCodeFirstApproach\bin\Debug\ServerEnvironments.xml" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" />
  </serverDetails>
</configuration>

And the splitted configuration files corresponding to two configuration objects defined in the code are generated automatically by the framework.

For the ServerEnvironments object, the file named ServerEnvironments.xml is generated as below

<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="serverDetails">
      <section name="environments" type="Cinchoo.Core.Configuration.Handlers.ChoNameValueSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
    </sectionGroup>
  </configSections>
  <serverDetails>
    <environments>
      <add key="Host" value="WNYC1000234011" />
      <add key="IP" value="205.12.5.122" />
      <add key="Port" value="2001" />
    </environments>
  </serverDetails>
</configuration>

For the ApplicationSettings object, the file named ApplicationSettings.xml is generated as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="applicationSettings" type="Cinchoo.Core.Configuration.Handlers.ChoNameValueSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
  </configSections>
  <applicationSettings>
    <add key="Mode" value="LIVE" />
    <add key="IsConnected" value="True" />
    <add key="Exchange" value="NYSE" />
  </applicationSettings>
</configuration>

For any reason, the configuration files needs to be re-created, all you have to do is delete the configuration files or removing the configuration sections from the configuration file will trigger the Cinchoo framework to regenerate them seamlessly.

2. Manual Step (Configuration model first approach)

This approach is suitable if you already have established configuration file, you can use this method to split them.

Lets say, you have application configuration file named HelloWorld.exe.xml 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="serverDetails" type="Cinchoo.Core.Configuration.Handlers.ChoNameValueSectionHandler, Cinchoo.Core" />
        <sectionGroup name="allSections">
            <section name="sample" type="Cinchoo.Core.Configuration.Handlers.ChoXmlSerializerSectionHandler, Cinchoo.Core" />
        </sectionGroup>
    </configSections>
    <serverDetails Name="Mark">
        <Message>Hello World!</Message>
    </serverDetails>
    <allSections>
        <sample Name="Mark">
            <Message>Hello World!</Message>
        </sample>
    </allSections>
</configuration>

Above configuration file contains two sections ‘serverDetails’ and ‘allSections/sample’. I’m going to split this file into two files and show you how to link them together in the main configuration file.

The main configuration file HelloWorld.exe.xml will look as below after the split

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <serverDetails cinchoo:path="ServerDetails.xml" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" />
    <allSections>
        <sample cinchoo:path="Sample.xml" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" />
    </allSections>
</configuration>

The splitted configuration files ServerDetails.xml and Sample.xml files will look below

ServerDetails.xml

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="serverDetails" type="Cinchoo.Core.Configuration.Handlers.ChoXmlSerializerSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
    </configSections>
    <serverDetails Name="Mark">
        <Message>Hello World!</Message>
    </serverDetails>
</configuration>

Sample.xml

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="allSections">
            <section name="sample" type="Cinchoo.Core.Configuration.Handlers.ChoXmlSerializerSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
        </sectionGroup>
    </configSections>
    <allSections>
        <sample Name="Mark">
            <Message>Hello World!</Message>
        </sample>
    </allSections>
</configuration>

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s