Cinchoo – Configuration framework, part 24

Binding to WinForm controls

Download Source Files (ZIP)

In this section, I’ll talk about binding your configuration object to WinForm controls. WinForm has rich binding infrastructure, Cinchoo framework works closely with WinForms to bind the configuration object to them seamlessly. Lets walk over how you can do this.

For a sample configuration object below

[ChoNameValueConfigurationSection("sample")]
public class SampleConfigSection : ChoConfigurableObject
{
    [ChoPropertyInfo("name", DefaultValue = "Mark")]
    public string Name
    {
        get;
        set;
    }

    [ChoPropertyInfo("message", DefaultValue = "Hello World!")]
    public string Message
    {
        get;
        set;
    }
}

PS: WinForm controls only binds to public Properties only. Please make sure you define them accordingly in your configuration object.

You can bind the above configuration object to your WinForm window in either constructor or window load event handler as below.

private void MainForm_Load(object sender, EventArgs e)
{
    SampleConfigSection settings = new SampleConfigSection();
    textBox1.DataBindings.Add("Text", settings, "Name");
    textBox2.DataBindings.Add("Text", settings, "Message");
}

That’s all. Now the changes made to configuration source will be reflected in the controls as well as the changes made to controls will be persisted automatically to underlying source based on the binding nature. Try for yourself.

Cinchoo – Configuration framework, part 23

Passing Configuration FilePath Programmatically

UPDATED: In this topic, I’m going to show you how to pass configuration object filepath programmatically. Cinchoo framework addresses this feature through IChoConfigurationParametersOverridable interface. When you define a configuration object, implement this interface as below to override the configuration file path.

[ChoNameValueConfigurationSection("applicationSettings")]
public class ApplicationSettings : ChoConfigurableObject
{
    [ChoPropertyInfo("path", DefaultValue = @"C:\")]
    public string Path;

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

    protected override void OverrideMetaDataInfo(ChoBaseConfigurationMetaDataInfo metaDataInfo)
    {
        ChoStandardConfigurationMetaDataInfo info = metaDataInfo as ChoStandardConfigurationMetaDataInfo;
        info.ConfigFilePath = @"C:\Test\ApplicationSettings.xml";
    }
}

Cinchoo – Configuration framework, part 22

Binding to WPF Controls

Download Source Files (ZIP)

In this section, I’ll talk about binding your configuration object to WPF controls. As WPF provides rich binding infrastructure, Cinchoo framework take a step closer to bind the configuration object to WPF controls seamlessly. Lets walk over how you can achieve this.

For a sample configuration object below

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

	[ChoPropertyInfo("message", DefaultValue="Hello World!")]
	public string Message;
}

You can bind the configuration object to your WPF window in either constructor or window loaded event handler as below.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    ChoWPFBindableConfigObject<SampleConfigSection> bindObj = new ChoWPFBindableConfigObject<SampleConfigSection>();
    this.DataContext = bindObj;
}

That’s it. Now you can bind each control to this context object members. For example, the below snippet binds a text box to ‘Name’ property of above configuration object.

<TextBox Name="txtValidExts" Text="{Binding Path=Name}" />

Now the changes made to configuration source will be reflected in the controls as well as the changes made to controls will be persisted automatically to underlying source based on the binding nature. Try for yourself.

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>