Configuration file reading. Best practice

Posted by Andrew Florko on Stack Overflow See other posts from Stack Overflow or by Andrew Florko
Published on 2010-05-19T05:58:58Z Indexed on 2010/05/19 6:10 UTC
Read the original article Hit count: 295

Filed under:
|
|
|

Application stores configuration data in custom section of configuration file. This information is used all over the application.

Nowadays I use helper static class to to provide access like this (some code omitted or simplified):

[XmlRoot("webSiteSection")]
public class WebSiteConfig : IConfigurationSectionHandler
{

    public static WebSiteConfig Current
    {
         get
         {          
             if (_current == null)
                _current = (WebSiteConfig) ConfigurationManager.GetSection("webSiteSection");

            return _current;
     }  
    }

    [XmlElement("section1")]
    public Section1 Section1 { get; set; }

    [XmlElement("section2")]
    public Section2 Section2 { get; set; }

    ...

    public object Create(object parent, object configContext, XmlNode section)
    {
        var serializer = new XmlSerializer(typeof(WebSiteConfig));
        return serializer.Deserialize(new XmlNodeReader(section));
    }
}

Then I use it like this

<%: WebSiteConfig.Current.Section1.Value1  %>
<%: WebSiteConfig.Current.Section1.Value2  %>

What do you think of it? I find it usable because it keeps code simple, but also confused as IConfigurationSectionHandler is deprecated since .NET Framework 2.0

© Stack Overflow or respective owner

Related posts about .NET

Related posts about applications