Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET custom configuration - Can I have elements collection with non-homogeneous elements?

I have a custom configuration section registered in the app/web.config, let's call it MySection. I have an ElementCollection element inside the section, called MyElements. Inside the element collection I want to have elements which are represented by different classes - the idea is that these are similar classes with some common properties and some specific to the instance.

Here is some xml configuration example:

<MySection>
  <MyElements>
    <Element1 name="someProp1" value="someValue" />
    <Element2 name="someProp2" format="{0}{1}" />
  </MyElements>
</MySection>

In my simple example, all elements must have a 'name' property, some will have also a 'value' property, and the other a 'format' property. Here, I want Element1 and Element2 to be represented in the .NET runtime by two different classes which have a common base class that defines the 'name' property.

As far as I have dug into .NET configuration, I got the impression that an element collection (like 'MyElements' here) should contain homogeneous elements (only of one type). So, could it be possible to achieve what I want - make it contain elements of different classes. The idea is to avoid both having more than one element collection for different element types and not to write all repeating properties for every custom ConfigurationElement implementation.

like image 425
Ivaylo Slavov Avatar asked Jan 03 '12 14:01

Ivaylo Slavov


1 Answers

You can achieve this by overriding OnDeserializeUnrecognizedElement method in your ElementCollection class and creating representations of your Element1 and Element2 by switching on tag name for ex. But AFAIR child elements should be derived from common ancestor anyway, doing it otherwise is too much trouble.

Define collection as:

public class MyElementCollection : ConfigurationElementCollection
{
    const string ELEMENT1 = "Element1";
    const string ELEMENT2 = "Element2";

    protected override ConfigurationElement CreateNewElement ()
    {
        return new MyElement (this);
    }

    protected override object GetElementKey (ConfigurationElement element)
    {
        return ((MyElement)element).Key;
    }

    // This method called when framework sees unknown element tag
    // inside the collection. You can choose to handle it yourself
    // and return true, or return false to invoke default action
    // (exception will be thrown).
    protected override bool OnDeserializeUnrecognizedElement (string elementName, XmlReader reader)
    {
        if (elementName == ELEMENT1 || elementName == ELEMENT2 {
            var myElement = new MyElement (this);

            switch (elementName) {
            case ELEMENT1:
                myElement.Type = MyElementType.Element1;
                break;
            case ELEMENT2:
                myElement.Type = MyElementType.Element2;
                break;
            }

            myElement.DeserializeElementForConfig (reader, false);
            BaseAdd (myElement);

            return true;
        }

        return false;
    }
}

And child element:

public enum MyElementType
{
    Element1,
    Element2,
}

public class MyElement : ConfigurationElement
{
    const string NAME = "name";
    const string VALUE = "value";
    const string FORMAT = "format";

    // keys should be unique, current collection count will do
    // the trick without introducing artificial keys
    public MyElement (ConfigurationElementCollection collection)
    {
        Key = collection.Count;
    }

    // note that this is not ConfigurationProperty
    public int Key { get; private set; }

    // note that this is not ConfigurationProperty
    public MyElementType Type { get; set; }

    [ConfigurationProperty(NAME)]
    public string Name {
        get { return (string)this [NAME]; }
    }

    [ConfigurationProperty(VALUE)]
    public string Value {
        get { return (string)this [VALUE]; }
    }

    [ConfigurationProperty(FORMAT)]
    public string Format {
        get { return (string)this [FORMAT]; }
    }

    // This is called when framework needs a copy of the element,
    // but it knows only about properties tagged with ConfigurationProperty.
    // We override this to copy our Key and Type, otherwise they will
    // have default values.
    protected override void Reset (ConfigurationElement parentElement)
    {
        base.Reset (parentElement);

        var myElement = (MyElement)parentElement;
        Key = myElement.Key;
        Type = myElement.Type;
    }

    // original ConfigurationElement have this protected,
    // redeclaring as protected internal to call it from collection class
    protected internal void DeserializeElementForConfig (XmlReader reader, bool serializeCollectionKey)
    {
        DeserializeElement (reader, serializeCollectionKey);
    }
}
like image 195
Oleg Kolosov Avatar answered Nov 15 '22 00:11

Oleg Kolosov