Cinchoo – Configuration framework, part 9

Using ChoRegistryConfigStorage

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

In this section, I’ll go over the details of using ChoRegistryConfigStorage in your application. It’s a custom configuration section handler enables application to read and write application settings from Windows System Registry.

1. Define the configuration section object decorated with ChoRegistryConfigurationSectionAttribute as below.

[ChoRegistryConfigurationSection("sample", "HKLM/Software/Sample")]
public class SampleConfigSection : ChoConfigurableObject
{
	#region Instance Data Members (Public)

	[ChoPropertyInfo("name", DefaultValue="Mark")]
	public string Name;

	[ChoPropertyInfo("message", DefaultValue="Hello World!")]
	public string Message;

	#endregion

	[ChoAfterConfigurationObjectLoadedHandler]
	void OnAfterConfigurationObjectLoaded(object sender, ChoConfigurationObjectEventArgs e)
	{
		Console.WriteLine(sender.ToString());
	}
}

2. Instantiate and use it as below.

class Program
{
	static void Main(string[] args)
	{
		SampleConfigSection sampleConfigSection = new SampleConfigSection();
		Console.WriteLine(sampleConfigSection.ToString());
	}
}

The configuration section will be generated automatically for the first time in HelloWorld.exe.xml as below.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <sample cinchoo:registryKey="HKLM\Software\sample" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" />
  <configSections>
    <section name="sample" type="Cinchoo.Core.Configuration.ChoDictionarySectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
  </configSections>
</configuration>

Also the corresponding registry entries are created automatically if not exists as below

Please visit the CodeProject article below for more detailed information about this storage.

Cinchoo – Using Windows registry as configuration source

Cinchoo – Configuration framework, part 8

Using ChoIniConfigStorage

UPDATE:

Please visit the below code project article for latest updates

Cinchoo – Using INI file as configuration source

 

 

In this section, I’ll go over the details of using ChoIniConfigStorage in your application. It’s a custom configuration section handler enables the applications to read and write its settings from INI file.

1. Define the configuration section object decorated with ChoIniConfigurationSectionAttribute as below.

[ChoIniConfigurationSection("sample", "SAMPLE_INI_SECTION", "Sample.ini")]
public class SampleConfigSection : ChoConfigurableObject
{
	#region Instance Data Members (Public)

	[ChoPropertyInfo("name", DefaultValue="Mark")]
	public string Name;

	[ChoPropertyInfo("message", DefaultValue="Hello World!")]
	public string Message;

	#endregion

	[ChoAfterConfigurationObjectLoadedHandler]
	void OnAfterConfigurationObjectLoaded(object sender, ChoConfigurationObjectEventArgs e)
	{
		Console.WriteLine(sender.ToString());
	}
}

2. Now instantiate and use it.

class Program
{
	static void Main(string[] args)
	{
		SampleConfigSection sampleConfigSection = new SampleConfigSection();
		Console.WriteLine(sampleConfigSection.ToString());
	}
}

The configuration section will be generated automatically for the first time in HelloWorld.exe.xml as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <sample cinchoo:iniSectionName="SAMPLE_INI_SECTION" cinchoo:iniFilePath="Sample.ini" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" />
  <configSections>
    <section name="sample" type="Cinchoo.Core.Configuration.ChoNameValueSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
  </configSections>
</configuration>

The corresponding meta-data looks as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <sample bindingMode="TwoWay" defaultable="true" silent="true">
    <configStorage>Cinchoo.Core.Configuration.ChoIniConfigStorage, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</configStorage>
    <logInfo condition="true" directory="Settings" fileName="ampleConfigSection.log" />
  </sample>
</configuration>

And the corresponding INI file and section will be created automatically if not exists as below

Please visit the CodeProject article below for more detailed information about this storage.

Cinchoo – Using INI file as configuration source

Cinchoo – Configuration framework, part 7

Using ChoStandardAppSettingsConfigStorage

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

In this section, I’ll go over the details of using ChoStandardAppSettingsConfigStorage in your application. It’s a custom configuration section handler provides the ability to read and write  custom application key/value-pair configuration information under <appSettings> section in App.config file.

1. Define the configuration section object decorated with ChoStandardAppSettingsConfigurationSectionAttribute as below.

[ChoStandardAppSettingsConfigurationSection]
public class SampleConfigSection : ChoConfigurableObject
{
	#region Instance Data Members (Public)

	[ChoPropertyInfo("name", DefaultValue="Mark")]
	public string Name;

	[ChoPropertyInfo("message", DefaultValue="Hello World!")]
	public string Message;

	#endregion

	[ChoAfterConfigurationObjectLoadedHandler]
	void OnAfterConfigurationObjectLoaded(object sender, ChoConfigurationObjectEventArgs e)
	{
		Console.WriteLine(sender.ToString());
	}
}

2. Now instantiate and use it as below.

class Program
{
	static void Main(string[] args)
	{
		SampleConfigSection sampleConfigSection = new SampleConfigSection();
		Console.WriteLine(sampleConfigSection.ToString());
	}
}

The configuration section will be generated automatically for the first time in HelloWorld.exe.config (please note that this section is NOT created in HelloWorld.exe.xml file) as below

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="name" value="Mark" />
    <add key="message" value="Hello World!" />
  </appSettings>
</configuration>

Cinchoo – Configuration framework, part 6

Using ChoSingleTagConfigStorage

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

In this section, I’ll go over the details of using ChoSingleTagConfigStorage in your application. The configuration sections are represented by a single XML tag from a file source.

1. Define the configuration section object decorated with ChoSingleTagConfigurationSectionAttribute as below.

[ChoSingleTagConfigurationSection("sample")]
public class SampleConfigSection : ChoConfigurableObject
{
	#region Instance Data Members (Public)

	[ChoPropertyInfo("name", DefaultValue="Mark")]
	public string Name;

	[ChoPropertyInfo("message", DefaultValue="Hello World!")]
	public string Message;

	#endregion

	[ChoAfterConfigurationObjectLoadedHandler]
	void OnAfterConfigurationObjectLoaded(object sender, ChoConfigurationObjectEventArgs e)
	{
		Console.WriteLine(sender.ToString());
	}
}

2. Now instantiate and use it as below.

class Program
{
	static void Main(string[] args)
	{
		SampleConfigSection sampleConfigSection = new SampleConfigSection();
		Console.WriteLine(sampleConfigSection.ToString());
	}
}

The configuration section will be generated automatically for the first time in HelloWorld.exe.xml as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="sample" type="Cinchoo.Core.Configuration.ChoNameValueSectionHandler, Cinchoo.Core" />
  </configSections>
  <sample name="Mark" message="Hello World!" />
</configuration>

Cinchoo – Configuration framework, part 5

Using ChoFileDictionaryConfigStorage

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

In this section, I’ll go over the details of using ChoFileDictionaryConfigStorage in your application. It provides key/value-pair configuration information from a file source. It is the default storage used by ChoDictionaryConfigurationHandler.

1. Define the configuration section object decorated with ChoDictionaryConfigurationSectionAttribute as below.

[ChoDictionaryConfigurationSection("sample")]
public class SampleConfigSection : ChoConfigurableObject
{
	#region Instance Data Members (Public)

	[ChoPropertyInfo("name", DefaultValue="Mark")]
	public string Name;

	[ChoPropertyInfo("message", DefaultValue="Hello World!")]
	public string Message;

	#endregion

	[ChoAfterConfigurationObjectLoadedHandler]
	void OnAfterConfigurationObjectLoaded(object sender, ChoConfigurationObjectEventArgs e)
	{
		Console.WriteLine(sender.ToString());
	}
}

2. Now instantiate and use it as below.

class Program
{
	static void Main(string[] args)
	{
		SampleConfigSection sampleConfigSection = new SampleConfigSection();
		Console.WriteLine(sampleConfigSection.ToString());
	}
}

The configuration section will be generated automatically for the first time in HelloWorld.exe.xml as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="sample" type="Cinchoo.Core.Configuration.ChoDictionarySectionHandler, Cinchoo.Core" />
  </configSections>
  <sample>
    <add key="name" value="Mark" />
    <add key="message" value="Hello World!" />
  </sample>
</configuration>

Cinchoo – Configuration framework, part 4

Using ChoFileNameValueConfigStorage

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

In this part, I’ll go over the details of using ChoFileNameValueConfigStorage in your application. It provides name/value-pair configuration information from file source. This is the default configuration storage used by ChoNameValueConfigurationSectionHandlerChoNameValueConfigurationSectionAttribute is the corresponding attribute used to decorate the configuration object class.

1. Define the configuration section object decorated with ChoNameValueConfigurationSectionAttribute as below.

[ChoNameValueConfigurationSection("sample")]
public class SampleConfigSection : ChoConfigurableObject
{
	#region Instance Data Members (Public)

	[ChoPropertyInfo("name", DefaultValue="Mark")]
	public string Name;

	[ChoPropertyInfo("message", DefaultValue="Hello World!")]
	public string Message;

	#endregion

	[ChoAfterConfigurationObjectLoadedHandler]
	void OnAfterConfigurationObjectLoaded(object sender, ChoConfigurationObjectEventArgs e)
	{
		Console.WriteLine(sender.ToString());
	}
}

2. Now instantiate and use it as below

class Program
{
	static void Main(string[] args)
	{
		SampleConfigSection sampleConfigSection = new SampleConfigSection();
		Console.WriteLine(sampleConfigSection.ToString());
	}
}

The configuration section will be generated automatically for the first time in HelloWorld.exe.xml as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="sample" type="Cinchoo.Core.Configuration.ChoNameValueSectionHandler, Cinchoo.Core" />
  </configSections>
  <sample>
    <add key="name" value="Mark" />
    <add key="message" value="Hello World!" />
  </sample>
</configuration>

Cinchoo – Configuration framework, part 3

Configuration file discovery

In this section, I’ll walk you over the discovery of the configuration file by Cinchoo framework. By default, Cinchoo framework creates or uses [appExeName].xml as the application configuration file.

In some cases, you may want to override this behavior and pass the application configuration file explicitly.

Configurable Way

Open App.config file under source directiory, add appFrxSettings custom section as below (absolute or relative path can be specified)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="appFrxSettings" type="Cinchoo.Core.ChoAppFrxSettings, Cinchoo.Core" />
    </configSections>
    <appFrxSettings appFrxFilePath="C:\ConfigFolder" />
</configuration>

Programmatic Way 

Register to ChoApplication.ApplyFrxParamsOverrides event to override the application configuration path. Example below

class Program
{
    static void Main(string[] args)
    {
        ChoApplication.ApplyFrxParamsOverrides += ChoApplication_ApplyFrxParamsOverrides;

        ChoConsole.Pause();
    }

    static void ChoApplication_ApplyFrxParamsOverrides(object sender, ChoFrxParamsEventArgs e)
    {
        e.AppFrxSettings.AppFrxFilePath = "C:\ConfigFolder";
    }
}

Cinchoo – Configuration framework, part 2

Configuration Binding

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

In this section, we’ll see how the changes are gets exchanged between configuration section object and the configuration file. The binding between them can be controlled either declaratively or through configuration.

Declarative way

BindingMode member of ChoConfigurationSectionAttribute drives the binding nature of the configuration section object. It is of ChoConfigurationBindingMode enum type.

  • TwoWay – (Default) The changes are flown both way between configuration section object and data source.
  • OneWay – The changes are flown only from data source to configuration section object.
  • OneWayToSource – The changes are flown only from configuration section object to data source.
  • OneTime – The configuration settings are read and loaded into configuration section object once. Any changes made either in the configuration section object or data source are not propagated between them.
If none specified, TwoWay  will be the default binding mode.
[ChoConfigurationSection("sample",
	BindingMode = ChoConfigurationBindingMode.TwoWay)]
public class SampleConfigSection : ChoConfigurableObject
{
	#region Instance Data Members (Public)

	[ChoPropertyInfo("name", DefaultValue = "Mark")]
	public string Name;

	[ChoPropertyInfo("message", DefaultValue = "Hello World!")]
	public string Message;

	#endregion
}
Configurable way

You can change the binding mode through configuration anytime without changing the code. Cinchoo framework will give precedence to it over declarative way.

In order to set the binding mode in the configuration, you can do so as below in HelloWorld.config.meta file, usually found in bin/Debug/Config/Meta or bin/Release/Config/Meta folder.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <sample bindingMode="TwoWay" defaultable="true" silent="true">
    <configStorage>Cinchoo.Core.Configuration.ChoNameValueSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</configStorage>
    <logInfo condition="true" directory="Settings" fileName="SampleConfigSection.log" />
  </sample>
</configuration>
Let’s see the actions

Now lets write code to change the value of name configuration property to ‘Tommy’, see it is saved to configuration file (Look at the HelloWorld.exe.xml under bin/Debug/Config or bin/Release/Config folder).

class Program
{
	static void Main(string[] args)
	{
		SampleConfigSection sampleConfigSection = new SampleConfigSection();
		sampleConfigSection.Name = "Tommy";

		Console.WriteLine("Press ENTER key to exit...");
		Console.ReadLine();
	}
}

After running this sample, you will find that the HelloWorld.exe.xml gets updated as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="sample" type="Cinchoo.Core.Configuration.Handlers.ChoNameValueSectionHandler, Cinchoo.Core">
        <metaData bindingMode="TwoWay" />
	</section>
  </configSections>
  <sample>
    <add key="name" value="Tommy" />
    <add key="message" value="Hello World!" />
  </sample>
</configuration>

Finally, lets get to see how the changes made to configuration file are loaded to the configuration section object. In order to see the loaded values, I’m going to override OnAfterConfigurationObjectLoaded method as below. This overridden method gets called whenever the object gets loaded with new values.

[ChoConfigurationSection("sample",
	BindingMode = ChoConfigurationBindingMode.TwoWay)]
public class SampleConfigSection : ChoConfigurableObject
{
	#region Instance Data Members (Public)

	[ChoPropertyInfo("name", DefaultValue="Mark")]
	public string Name;

	[ChoPropertyInfo("message", DefaultValue="Hello World!")]
	public string Message;

	#endregion

    protected override void OnAfterConfigurationObjectLoaded()
    {
        Console.WriteLine(ToString());
    }
}

One another way to see the values of loaded configuration values, you will need to add a OnAfterConfigurationObjectLoaded method to the configuration section class as below, so that it will get triggered as soon as the section gets changed its state. This method should be decorated with ChoAfterConfigurationObjectLoadedHandlerAttribute in order to register with framework.

[ChoConfigurationSection("sample",
	BindingMode = ChoConfigurationBindingMode.TwoWay)]
public class SampleConfigSection : ChoConfigurableObject
{
	#region Instance Data Members (Public)

	[ChoPropertyInfo("name", DefaultValue="Mark")]
	public string Name;

	[ChoPropertyInfo("message", DefaultValue="Hello World!")]
	public string Message;

	#endregion

	[ChoAfterConfigurationObjectLoadedHandler]
	void OnAfterConfigurationObjectLoaded(object sender, ChoConfigurationObjectEventArgs e)
	{
		Console.WriteLine(sender.ToString());
	}
}

Run this sample, and change the name configuration property value to ‘Cathy’ in the configuration file (HelloWorld.exe.xml). You will notice that the configuration section object gets reflected with new value as below

-- HelloWorld.SampleConfigSection State --
        Name: Tommy
        Message: Hello World?

Press ENTER key to exit...
-- HelloWorld.SampleConfigSection State --
        Name: Cathy
        Message: Hello World?
Conclusion
In this section we learned about the binding mode, how and where to specify it, and finally we saw how the changes are exchanged between configuration section object and configuration file.

Cinchoo – Application configuration made easy

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;
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;
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>