Introduction to Configuration Framework
Download sample source files (Require .NET 4.0 / Visual Studio 2010)
Cinchoo – Application configuration is one of the feature Cinchoo framework provides for the developer. Application configuration is the information that application reads/writes at run time from the source. .NET already provides extensive support through several predefined configuration section handlers for the developers. But require some pluming development work on top of them to complete the exercise.
Cinchoo framework simply them with less code and read/write configuration values to the underlying data sources seamlessly. Also it gives you the flexibility of interchanging the configuration sources through configuration without any code change. In the following sections, I’m going to provide you in detail on how to use them in your application.
Cinchoo configuration framework have the below powerful features
- One single API to deal with configuration data with multiple sources
- Two-way, type-safe configuration management
- Create the configuration files, if those are missing
- Generate the configuration sections, if not exists
- During application run time, has the flexibility of changing the configuration sources without any restart or downtime needed.
- Auto correct the section if it missing any entries in the configuration files.
Namespace: Cinchoo.Core.Configuration
The heart of the Cinchoo configuration framework is in Cinchoo.Core.Configuration namespace. This namespace is available when you reference Cinchoo.Core.dll assembly in your project.
PS: Please turn on Reflection Permission in order to use this framework.
Writing a simple configuration section
Writing code to provide custom configuration section to your application is very simple and straightforward. Vast bulk of dealing with configuration data is done by Cinchoo framework. Lets start by creating a simple configuration section with two string values as below. Every configuration section must derive from ChoConfigurableObject and must specify the xpath of the configuration section using ChoConfigurationSectionAttribute available in the framework.
using System;
using Cinchoo.Core.Configuration;
[ChoConfigurationSection("sample")]
public class SampleConfigSection : ChoConfigurableObject
{
//Declare and expose the configuration properties here
}
Ok, once you start with configuration section class, you will need to define and expose the configuration properties by defining public fields / properties inside section class. By default, all public properties and fields will be treated as configuration properties. Each class member may be decorated using ChoPropertyInfoAttribute with the property name, default value, fallback value etc. Framework binds the members to the configuration attributes by the name.
Let’s start filling in the code to make our sample section to function.
using System;
using Cinchoo.Core.Configuration;
[ChoConfigurationSection("sample")]
public class SampleConfigSection : ChoConfigurableObject
{
[ChoPropertyInfo("name", DefaultValue="Mark")]
public string Name;
[ChoPropertyInfo("message", DefaultValue="Hello World!")]
public string Message;
}
Once you are done, that’s it. This sample section is ready to use.
Available Configuration Section handlers
Cinchoo framework provides the following configuration section handler out of the box. Please visit Available Configuration Section Handlers section for the list.
Finally, let use the configuration in application
At last, lets see how to instantiate and use the configuration values in an application.
class Program
{
static void Main(string[] args)
{
SampleConfigSection sampleConfigSection = new SampleConfigSection();
Console.WriteLine(sampleConfigSection.ToString());
}
}
When you run this sample, the output will be shown as below
-- HelloWorld.SampleConfigSection State --
Name: Mark
Message: Hello World?
Press any key to continue . . .
The framework will create a HelloWorld.exe.xml configuration file automatically in the binary folder [at bin/Debug/Config or bin/Release/Config], if not exists. It outputs all the needed configuration entries to the configuration file.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="sample" type="Cinchoo.Core.Configuration.ChoNameValueSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
</configSections>
<sample>
<add key="name" value="Mark" />
<add key="message" value="Hello World!" />
</sample>
</configuration>