Click here to Skip to main content
12,452,106 members (58,588 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

3.1K views
11 downloads
2 bookmarked
Posted

Cinchoo - Support of Complex Object as Configuration Value

, 8 Sep 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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:
[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:

[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:

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

Share

About the Author

Cinchoo
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 8 Sep 2013
Article Copyright 2013 by Cinchoo
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid