Cinchoo – Configuration framework, part 21

Configuration Storage Plug-in

This article is the continuation of Cinchoo configuration framework series. So far you learned and used various configuration section handler provided by Cinchoo framework in your applications. But it limits the accessibility of the underlying configuration data source somehow. Now Cinchoo framework opens the possiblity of extending the configuration framework by creating your own plug-ins to access various configuration sources by using IChoDictionaryConfigObjectAdapter interface.

Let say, you have configuration data available in Database, FTP, HTTP, WebService, WCF, email etc. Now those can be accessed easily using Cinchooframework. Here I’m going to walk you over in implementing one such configuration data source reside in database using ADO.NET interface. (You can try Linq to Sql, ADO.NET Entity Framework etc as well).

IChoDictionaryConfigObjectAdapter interface exploses the below members

public interface IChoDictionaryConfigObjectAdapter
{
    IDictionary GetData();
    void Save(IDictionary dict);
    DateTime LastUpdateDateTime
    {
        get;
    }
}

Here are the steps to create a new adapter class

  1. Create a new class (say, ChoMySqlADODictionaryConfigObjectAdapter) derived from IChoDictionaryConfigObjectAdapter interface. Implement the interface members.
  2. GetData() member will be called by framework to retrieve the configuration data. In here you establish connection to underlying source, retrieve the data and return them in IDictionary format.
  3. Save() method will be called by framework to save the snapshot (in IDictionary format) of configuration data back to underlying source. Here you establish the connection to the underlying source, save them. Important, while saving, make sure you update the modified timestamp with current timestamp.
  4. LastUpdateDateTime property will be invoked periodically by framework to identify the changes to the underlying configuration source. In here, you will have to establish the connection to the source and retrieve the modified datatime.
public class ChoMySqlADODictionaryConfigObjectAdapter : IChoDictionaryConfigObjectAdapter
{
    public IDictionary GetData()
    {
        Dictionary<string, object> dict = new Dictionary<string, object>();

        //Connect to the source, load the configuration object payload

        return dict;
    }

    public void Save(System.Collections.IDictionary dict)
    {
        if (dict != null && dict.Count > 0)
        {
            //Connect to the source, save the data
        }
    }

    public DateTime LastUpdateDateTime
    {
        get
        {
            //Connect to the source, get the last updated timestamp
            return DateTime.MinValue;
        }
    }
}

Now here is how to use the above adapter in your application

Define a configuration class ‘ApplicationSettings’, decorated with ChoDictionaryAdapterConfigurationSection attribute as below with sectionName, and the type of the adapter ChoMySqlADODictionaryConfigObjectAdapter. That’s all. Your configuration object is all set to use the configuration object data from database using ChoMySqlADODictionaryConfigObjectAdapter.

[ChoDictionaryAdapterConfigurationSection("mySqlDictionarySectionHandlerTest/applicationSettings", typeof(ChoMySqlADODictionaryConfigObjectAdapter))]
public class ApplicationSettings : ChoConfigurableObject
{
    #region Instance Data Members (Public)

    [ChoPropertyInfo("path", DefaultValue = @"C:\")]
    public string Path;

    [ChoPropertyInfo("OS", DefaultValue = "Windows")]
    public string OS;

    [ChoPropertyInfo("singleInstanceApp", DefaultValue = false)]
    public bool SingleInstanceApp;

    #endregion
}

Here is how you can instantiate and use the above configuration object

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

    ChoFramework.Shutdown();
}

The corresponding configuration section will be created automatically in [appExeName].xml file under bin/Config folder

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <mySqlDictionarySectionHandlerTest>
    <applicationSettings cinchoo:configObjectAdapterType="Cinchoo.Core.Configuration.ChoMySqlADODictionaryConfigObjectAdapter, ChoMySqlADODictionaryConfigStorage, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" />
  </mySqlDictionarySectionHandlerTest>
  <configSections>
    <sectionGroup name="mySqlDictionarySectionHandlerTest">
      <section name="applicationSettings" type="Cinchoo.Core.Configuration.ChoDictionarySectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
    </sectionGroup>
  </configSections>
</configuration>
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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s