Geeks With Blogs
Paul's Petrov Whiteboard [BizTalk, Enterprise Application Integration, Business Process Automation, SOA, .NET]

Simple trick that saves me some time and coding when writing custom configuration objects is to define generic collection for them. Let's say I created several custom configuration elements derived from my CustomConfigElementBase which in turn inherits from System.Configuration.ConfigurationElement. Instead of defining strongly typed collections for each type I can simply define class:

public class GenericConfigElementCollection<T> : ConfigurationElementCollection

where T : CustomConfigElementBase, new()

{

  public T this[int index]

  {

    get

    {

      return base.BaseGet(index) as T;

    }

    set

    {

      if (base.BaseGet(index) != null)

       base.BaseRemoveAt(index);

      base.BaseAdd(index, value);

    }

  }

  protected override ConfigurationElement CreateNewElement()

  {

    return new T();

  }

  protected override object GetElementKey(ConfigurationElement element)

  {

    return ((T)element).ElementKey;

  }

}

 

Note, that base custom configuration element has property ElementKey to satisfy GetElementKey method implementation. If your custom elements have different keys, you can override this property. It's possible to skip CustomConfigElementBase and define generic collection with ConfigurationElement type restriction. GetElementKey implementation would use reflection to retreive key property:

public class GenericConfigElementCollection<T> : ConfigurationElementCollection

where T : ConfigurationElement, new()

{

  protected override object GetElementKey(ConfigurationElement element)

  {

    //- find and return property which is a key for a given type T using reflection

    ...

  }

}

Now, when we define configuration section we just return generic collection of appropriate type:

 

    public class MyCustomConfigSection : ConfigurationSection

    {

        [ConfigurationProperty("myCustomSettings", IsRequired = true)]

        public GenericConfigElementCollection<MyCustomConfigElement> MyCustomSettings

        {

            get

            {

                return this["myCustomSettings"]

                  as GenericConfigElementCollection<MyCustomConfigElement>;

            }

        }

    }

And then just use it in an application code:

GenericConfigElementCollection<MyCustomConfigElement> configCollection = configSection.MyCustomSettings;

MyCustomConfigSection configSection = (MyCustomConfigSection)ConfigurationManager.GetSection("mySection");

 

Posted on Tuesday, November 13, 2007 1:32 PM .Net , Software Engineering | Back to top


Comments on this post: Generic custom configuration elements collection based on ConfigurationElementCollection

# re: Generic custom configuration elements collection based on ConfigurationElementCollection
Requesting Gravatar...
What does the CustomConfigElementBase class look like?

Left by Lenny Miller on Jan 27, 2008 5:13 PM

# re: Generic custom configuration elements collection based on ConfigurationElementCollection
Requesting Gravatar...
Lenny,

CustomConfigElementBase is your own implementation of System.Configuration.ConfigurationElement class. It defines whatever properties you want to be inherited by all custom configuration elements. In my example, it's an ElementKey property. If your elements don't have shared properties, you can use ConfigurationElement but in generic collection GetElementKey() method implementation you'd use reflection to return current type's key.

The whole idea described makes sense if you have many custom configuration elements. It saves some repetitive coding, i.e. defining strongly typed collection of each custom element type created. In case of one or two custom elements we don't gain much, just use regular approach with typed collections.
Left by Paul Petrov on Jan 28, 2008 10:13 PM

# re: Generic custom configuration elements collection based on ConfigurationElementCollection
Requesting Gravatar...
How to this value, not the attribute?

<MyCustomSettings>
<add>Value</add>
</MyCustomSettings>
Left by Xor2 on Apr 08, 2008 8:05 AM

Your comment:
 (will show your gravatar)


Copyright © Paul Petrov | Powered by: GeeksWithBlogs.net