Cinchoo – Consuming Xml as configuration value

Download Sample.zip – 4.6 KB

1. Introduction

Cinchoo is the application framework for .NET. One of the main functionality it provides to the users is the application configuration management. Application configuration is the information that 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 consuming xml data as configuration value. In general, all the configuration values are simple and intrinsic. Sometimes we may want to manage read and save xml 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 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 AppSettings : ChoConfigurableObject
{
    [ChoPropertyInfo("name", DefaultValue = "Mark")]
    public string Name;

    [ChoPropertyInfo("address")]
    public string Address;
}

The code above illustrates about defining configuration object. First thing define a configuration (ex. 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 available configuration sections except ChoSingleTagConfigurationSection, which does not support complex configuration values. 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 name, which will consume the payload in xml format from configuration file. Once you have the configuration object defined as above, it is now ready to use in your application. Here is how you can create and use it.

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
    appSettings.Address = "<address><street>10 River Road</street><city>New York</city><state>NY</state></address>";

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

Cinchoo framework automatically checks and identifies the payload of each configuration values and consume 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