Cinchoo – Using Array/Collections in Configuration Object

How to use Array or Collections in configuration object

Download sample source files

Cinchoo – Application configuration is one of the feature Cinchoo framework provides for the developer.  Application configuration is the information that application reads/writes at run time from the source. .NET already provides extensive support through several predefined configuration section handlers for the developers. But require some pluming development work on top of them to complete the exercise. Cinchoo framework simply them with less code and read/write configuration values to the underlying data sources seamlessly.

In this section, I’m going to detailed out on using Array or Collection objects in your configuration object. ChoXmlSerializerConfigurationSection is suitable for this type of needs. It is a section used to consume configuration object containing user defined objects. In here, we going to explore a way to consume array of objects from configuration source.

Listing 1.1 Configuration Object

public class AppSettings : ChoConfigurableObject
    [ChoPropertyInfo("name", DefaultValue = "Raj")]
    public string Name;

    public ObservableCollectionEx<Employee> Employees = new ObservableCollectionEx<Employee>();

    protected override bool OnBeforeConfigurationObjectLoaded()
        if (Employees != null)

        return base.OnBeforeConfigurationObjectLoaded();

    protected override void OnAfterConfigurationObjectLoaded()
        if (Employees != null)


The code above illustrates about defining configuration object. It contains two members, name and Employees. name is a simple string configuration member. Cinchoo does the job of consuming it from source out of the box. The next member, Employees, is the collection of Employee objects. In order to consume from the source, it can be defined as List<T>, Array or ObservableCollection. But the problem here is the receiving change notification when collection changed or when one of its item gets changed. That’s why I’ve created a modified version of ObservableCollection class, to meet the purpose of two way binding. Take a look at the attached sample for its implementation. Besides that you must decorate this member with XmlArrayAttribute.

Next you need to take care few house keeping tasks of managing this collection member. Override OnBeforeConfigurationObjectLoaded method, to clear the collection before the configuration object is loaded from the source. On to next, override OnAfterConfigurationObjectLoaded method to wire the collection changed event to property changed event. I know it is kind of confusing. Read the sample to get clarity on it.

Now let’s define and consume the object as shown below

Listing 1.2 Main Method

static void Main(string[] args)
    AppSettings appSettings = new AppSettings();

    int random = Math.Abs(ChoRandom.NextRandom());
    Employee e1 = new Employee() { Id = random, Name = "Name{0}".FormatString(random) };


    foreach (Employee o in appSettings.Employees)
        o.Name = o.Name + "1";


Build and run the sample, the configuration file looks as expected below

Listing 1.3 Configuration Output

<?xml version="1.0" encoding="utf-8"?>
    <section name="appSettings" type="Cinchoo.Core.Configuration.ChoXmlSerializerSectionHandler, Cinchoo.Core" />
      <Employee Id="1416154546" Name="Name141615454611" />
      <Employee Id="1121887979" Name="Name11218879791" />

That’s all, the object is ready to be used to consume collections. Download and try for yourself. Thanks.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

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