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.
Advertisements

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 )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s