Monthly Archives: September 2011

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


Follow

Get every new post delivered to your Inbox.