Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Cinchoo - Support of Complex Object as Configuration Value

5.00/5 (2 votes)
8 Sep 2013CPOL2 min read 6K   15  
Consuming and storing complex user defined object as configuration member values

Introduction

Cinchoo is an application framework for .NET. One of the main functionalities it provides to users is the application configuration management. Application configuration is information that an application reads and/or writes at run-time from the source.

Please visit jump start article [Cinchoo - Simplified Configuration Manager] for more information about Cinchoo configuration manager.

In this section, I’ll detail about a new feature, using and saving custom user defined object as configuration values. So far, Cinchoo framework supports only scalar text values. It limits the configuration object member values from having any complex objects.

In standard .NET Framework, it is not supported in a straight way. You will have to create custom configuration section and put a plumbing code to parse the values. In Cinchoo framework, it is simplified to handle the complex objects as configuration values.

Here is how you can define configuration members to support custom user defined type.

How to Use

  • Download latest Cinchoo binary here.
  • Open VS.NET 2010 or higher.
  • Create a sample VS.NET (.NET Framework 4) Console Application project.
  • Add reference to Cinchoo.Core.dll
  • Use the Cinchoo.Core.Configuration namespace.
  • Define a configuration object 'AppSettings' as below:
C#
[ChoNameValueConfigurationSection("appSettings")]
public class AppSettings : ChoConfigurableObject
{
	[ChoPropertyInfo("name", DefaultValue = "Tom")]
	public string Name;

	[ChoPropertyInfo("address", 
	DefaultValue="10, River Road, Piscataway, NJ 08880 && New York")]
	public ChoCDATA Address;

	[ChoPropertyInfo("employer", 
	DefaultValue = "<Sample>ABCD Inc.</Sample>")]
	public string Employer;

	[ChoPropertyInfo("department")]
	public Department Department;

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

In the above sample configuration object, Department member is defined as Department type, it is defined as below:

C#
[ChoXmlSerializerConverter(typeof(Department))]
public class Department : INotifyPropertyChanged
{
	private int _deptCode;
	[XmlAttribute("deptCode")]
	public int DeptCode
	{
		get { return _deptCode; }
		set
		{
			_deptCode = value;
			RaisePropertyChanged("DeptCode");
		}
	}
	private string _deptName;
	[XmlAttribute("deptName")]
	public string DeptName
	{
		get { return _deptName; }
		set
		{
			_deptName = value;
			RaisePropertyChanged("DeptName");
		}
	}
	public event PropertyChangedEventHandler PropertyChanged;
	protected void RaisePropertyChanged(string propertyName)
	{
		if (propertyName.IsNullOrWhiteSpace()) return;
		PropertyChangedEventHandler propertyChanged = PropertyChanged;
		if (propertyChanged != null)
			propertyChanged(this, new PropertyChangedEventArgs(propertyName));
	}
}  

Key things to remember when you define custom object in order to use in configuration object:

  • Should be decorated as ChoXmlSerializerConverter which helps to serialize the object to XML string.
  • Must implement INotifyPropertyChanged in order to notify member changes to Configuration Manager.
  • Decorate appropriate XML serialization attribute to its members (i.e., XmlAttribute, XmlElement, etc.)

Now let's try to create and consume the above configuration object. The below code shows how to consume and modify the object members:

C#
static void Main(string[] args)
{
	AppSettings appSettings = new AppSettings();
	//Modify the members
	appSettings.Address.Value = "11, Oak Road, Woodbridge, NJ 08827";
	appSettings.Employer = "<Sample1>ZZZ1 Inc.</Sample1>";
	if (appSettings.Department == null)
		appSettings.Department = new Department() 
		{ DeptCode = 10, DeptName = "Comp Sc." };
	else
		appSettings.Department.DeptCode = appSettings.Department.DeptCode + 20; 
	//Console.WriteLine(appSettings.ToString());
	ChoConsole.PauseLine();
	ChoFramework.Shutdown();
} 

If you compile and run this sample, the output of the configuration will be seen as below:

XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="appSettings" 
    type="Cinchoo.Core.Configuration.ChoNameValueSectionHandler, Cinchoo.Core" />
  </configSections>
  <appSettings>
    <add key="name" value="Tom" />
    <add key="address">
      <value><![CDATA[11, Oak Road, Woodbridge, NJ 08827]]></value>
    </add>
    <add key="employer">
      <value>
        <Sample1>ZZZ1 Inc.</Sample1>
      </value>
    </add>
    <add key="department">
      <value>
        <Department deptCode="11" deptName="Comp Sc." />
      </value>
    </add>
  </appSettings>
</configuration>

That’s it folks, it is as easy as to store and consume custom user defined member in Configuration object.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)