Tuesday, February 12, 2008

IConfigurationSectionHandler is deprecated on .NET 2.0

I just heard a bad news while reading MCTS Book and MSDN.

It said that IConfugurationSectionHandler is deprecated on .Net 2.0.

We need to inherits from ConfigurationSection instead of implementing IConfigurationSectionHandler.

Somehow it is easier using ConfigurationSection but it is less customization.
  1. We can not have CDATA or text Element anymore. All value need to be placed on attribute.
    Otherwise it will throw this exception
    "The configuration section cannot contain a CDATA or text element."

    If implementing IConfigurationSectionHandler we can easily using XmlElement Declaration . [XmlElement("ElementName")]

  2. for multiple element values , We need to write ConfigurationElementCollection.

    public class ValueCollection : ConfigurationElementCollection
    {
    protected override ConfigurationElement CreateNewElement()
    {
    return new ValueElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
    return ((ValueElement)element).Value;
    }
    }

    public class ValueElement : ConfigurationElement
    {
    [ConfigurationProperty("value")]
    public string Value
    {
    get { return (String)this["value"]; }
    set { this["value"] = value; }
    }
    }

    [ConfigurationProperty("domains")]
    public ValueCollection Domains
    {
    get { return (ValueCollection)this["domains"]; }
    }

    Or you can use default configuration collection such as NameValueConfigurationCollection or KeyValueConfigurationCollection


    But If using IConfigurationSectionHandler we can just use XmlElementAttribute
    [XmlElement("ExtractColumn")]
    public ExtractColumn[] ExtractColumns;

====================
However The nice thing of using ConfigurationSection is
  1. Standard .Net Configuration
  2. Easier to validate and better error Messages
    [ConfigurationProperty("myAttrib1", DefaultValue = "test", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]

No comments: