Cinchoo – Consume JSON format configuration values

1. Introduction

Cinchoo is an application framework for .NET. One of the main functionality 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’m going to show you how to consume JSON data as configuration values using the Cinchoo configuration framework. It gives an opportunity to use industry standard message format as configuration source.

Let’s start this exercise using the below values defined in test.json file. This file can be places in any folder. You must specify the absolute path of this file while defining configuration object. Please note, if the file is not present, it will be created automatically the very first time the application starts.

Listing 1.1 Sample JSON file (test.json)

{"name":"Mark","address":"10 River Road, 08837","RunDate":"8\/16\/2015 6:16:36 PM"}

2. How to use

  • Open VS.NET 2010 or higher
  • Create a sample VS.NET (.NET Framework 4) Console Application project.
  • Download the latest Cinchoo.Core.Configuration.JSON via nuget. (Nuget Command: Install-Package Cinchoo.Core.Configuration.JSON)
  • Use the Cinchoo.Core.Configuration namespace
  • Define a configuration object ‘ApplicationSettings‘ as below.

Listing 2.1 Defining Configuration Object

[ChoJSONConfigurationSection("appSettings", ConfigFilePath = "test.json")]
public class ApplicationSettings : ChoConfigurableObject
{
    [ChoPropertyInfo("name", DefaultValue = "Mark")]
    [ChoNotNullOrWhiteSpaceValidator]
    public string Name
    {
        get;
        set;
    }

    [ChoPropertyInfo("address", DefaultValue = "10 River Road, 08837")]
    public string Address;

    [ChoPropertyInfo("Date", DefaultValue = "%%NOW^MM/dd/yyyy HH:mm:ss%%")]
    public DateTime RunDate;

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

The code above illustrates about defining configuration object. First thing defines a configuration object (ex. ApplicationSettings) class from ChoConfigurableObject, it indicates that this object is a configuration object. And it must be decorated with ChoJSONConfigurationSectionAttribute to complete the definition. This attribute enables the configuration object to read and write configuration values from ‘test.json’ file.

Define three members Name, Address and RunDate 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 property is given name as property name with defaultvalue as ‘Mark‘. It means that this member will be defaulted to default value when the value is missing in the configuration file. Respectively declare other members for each appSettings key. It is a very simple and clear way of specifying configuration members using attributes.

Once you have class declared as above, it is now ready to consume ‘test.json’ values as simple as creating object out of it. Any changes made to this object and/or in the test.json file will be exchanged automatically. The sample code is as below:

Listing 2.2 Main method

static void Main(string[] args)
{
    ApplicationSettings applicationSettings = new ApplicationSettings();
    Console.WriteLine(applicationSettings.Name);
    ChoConsole.PauseLine();
}

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

Just compile and run the above sample, it will output as below:

Listing 2.3 Output of Test1.exe

-- Cinchoo.Core.Configuration.JSON.Test.AppSettings State --
        Name: Mark
        Address: 10 River Road, 08837
        RubDate: 8/17/2015 10:02:08 PM

Press ENTER key to continue...

Now let’s see how the application picks up the changes made to the test.json file reflected in the output. Open test.json file, edit the name from ‘Mark‘ to ‘Tom‘ and save the file.

Listing 2.4 Output of Test1.exe when changing the values in the App.Config file

-- Cinchoo.Core.Configuration.JSON.Test.AppSettings State --
        Name: Mark
        Address: 10 River Road, 08837
        RubDate: 8/17/2015 10:02:08 PM

Press ENTER key to continue...
-- ChoStandardAppSettingsConfig.Test.ApplicationSettings State --
        Name: Tom
        Address: 10 River Road, 08837
        RubDate: 8/17/2015 10:02:08 PM

That’s all. A sample project is attached. Download and try for yourself.

Advertisements

2 thoughts on “Cinchoo – Consume JSON format configuration values

  1. ChoJSONConfigurationSection does not seem to be part of the Cinchoo.Core Assembly. I also cannot find a Nuget for Cinchoo JSON support. Perhaps you can share some details where you can get the assembly with JSON support

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