Cinchoo – Support of UDT as Configuration values

Download Sample.zip – 4.4 KB

1. Introduction

Cinchoo is the application framework for .NET. One of the main functionalities it provides to the users is the application configuration management. Application configuration is the information that the 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 run through with sample in defining and using User Defined Types (UDT) as configuration values. In general, all the configuration values are simple and intrinsic. Sometimes, we may want to manage read and save complex UDT data objects data.

2. Requirement

This configuration library is written in C# for the .NET 4.0 Framework. It is part of Cinchoo framework, which is a great library with lot of features like Configuration Management, common ApplicationHost, Shell features, etc.

3. “Hello World!” Sample

Let’s begin by looking into a simple example of an application consuming two configuration values name and address. name is required configuration value and address is a another required configuration value containing structured XML data as defined below.

Listing 3.1 Sample configuration file

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="appSettings" type="Cinchoo.Core.Configuration.ChoNameValueSectionHandler,
       Cinchoo.Core, Version=1.0.1.1, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
  </configSections>
  <appSettings>
    <add key="name" value="Mark" />
    <add key="address">
        <value>
            <address>
                <street>10 River Road</street>
                <city>New York</city>
                <state>NY</state>
            </address>
        </value>
  </appSettings>
</configuration>
  • Download the latest Cinchoo binary here. (Nuget Command: Install-Package Cinchoo)
  • 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 and Cinchoo.Core.Configuration namespace
  • Copy and paste the below command line object

Listing 3.2 Defining Configuration Object

[ChoNameValueConfigurationSection("appSettings")]
public class ChoApplicationSettings : ChoConfigurableObject
{
    [ChoPropertyInfo("name", DefaultValue = "Mark")]
    public string Name;

    [ChoPropertyInfo("Address")]
    public Address Address;

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

The code above illustrates about defining configuration object. First thing defines a configuration (example, AppSettings) class from ChoConfigurableObject, it indicates that this object is a configuration object. And it must be decorated with one of the available configuration section attributes, will be discussed later. In this example, I’ve used ChoNameValueConfigurationSectionAttribute with appSettings as section name to complete the definition. You can use any configuration sections available in Cinchoo framework. It specifies that this configuration section is the NameValueConfigurationSection. With this definition, we turned the above configuration object provides name/value-pair configuration information from a configuration section.

Define configuration value members Name and Address either as public fields or properties with get and set in it. Decorate them with ChoPropertyInfo attribute to indicate that they are configuration members. In this sample, Name is the optional configuration member. It is given nameas configuration member name with the DefaultValue as Mark‘. It means that if the configuration value is missing from the source, it will be defaulted to DefaultValue.

Address configuration member is an optional member as well. It was given with addressas configuration name, which will consume the payload in XML format from configuration file. It is of Address user defined type. Cinchoo framework makes it simplified the way of consuming/saving the structured Xml data into user defined type from configuration file. Below is the definition of Address UDT type.

Listing 3.2 Defining Address UDT type

public class Address : ChoEquatableObject<Address>
{
    private string _street;
    public string Street
    {
        get { return _street; }
        set
        {
            if (value != _street)
            {
                _street = value;
                RaisePropertyChanged("Street");
            }
        }
    }

    private string _city;
    public string City
    {
        get { return _city; }
        set
        {
            if (value != _city)
            {
                _city = value;
                RaisePropertyChanged("City");
            }
        }
    }

    private string _state;
    public string State
    {
        get { return _state; }
        set
        {
            if (value != _state)
            {
                _state = value;
                RaisePropertyChanged("State");
            }
        }
    }
}

The code above illustrates about defining Address UDT type. It must be derived from ChoEquatableObject, it indicates that this object will notify its changes to configuration framework. It defines three members (Street, City and State), with get and set accessors. Provides property change notifications via INotifyPropertyChanged pattern, which abstracted inside ChoEquatableObject. Once you have these change notification implemented for all properties, this object is eligible for sending all changes to the framework.

Listing 3.3 Main Method

static void Main(string[] args)
{
    AppSettings appSettings = new AppSettings();
    Console.WriteLine(appSettings.ToString());

    //Update the address value in xml value

    if (applicationSettings.Address == null)
        applicationSettings.Address = new Address() { Street = "10 River Road", City = "New York", State = "NY" };


    ChoConsole.PauseLine();
}

We start by creating a new instance of AppSettings object, That’s all. All the heavy lifting of loading configuration values to the object is done by the framework under the hood.

Now compile this program and run. It will create the section in [appExeName].xml (HelloWorld.exe.xml) file under bin/Config folder.

Listing 3.4 HelloWorld.exe.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="Mark" />
    <add key="Address">
      <value>
        <Address>
          <State>NY</State>
          <Street>10 River Road</Street>
          <City>New York</City>
        </Address>
      </value>
    </add>
  </appSettings>
</configuration>

Cinchoo framework automatically checks and identifies the payload of each configuration values and consumes them safely.

It is very simple to work with configuration using Cinchoo framework. Try for yourself. Thanks.

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