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

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