This project is read-only.
Goodstuff supports easy access to custom configuration sections. It is based on simple XML serialization instead of the more elaborate Config Section API.

To declare a configuration section class, simply implement a plain old object that contains your section:

public class DemoConfiguration
{
    public string Server { get; set; }
    public int TimeOut { get; set; }
}

Next, you will need to declare a configuration section handler to get access to that configuration. It is needed, because it is not possible declare a generic class in the web.config file:

public class DemoConfigurationSectionHandler : GoodStuff.ConfigurationSectionHandler<DemoConfiguration>
{
}

Now you need to add the reader and the section to the configuration file:
<configuration>
    <configSections>
        <section name="mySection" type="YourNameSpace.DemoConfigurationSectionHandler, YourNameSpace"/>
    <configSections>

    <mySection>
        <Server>http://goodstuff.codeplex.com</Server>
        <TimeOut>60</TimeOut>
    </mySection>
</configuration>

To read your configuration, simply use the helper.
DemoConfiguration configuration = GoodStuff.ConfigurationHelper.GetSection<DemoConfiguration>("mySection");

Console.WriteLine(configuration.Server);

If you don't like the casing of the properties, you can either make the properties lowercase, or use XmlSerialization attributes to specify the name of the property to be used in the configuration file, for example:

public class DemoConfiguration
{
    [XmlElement("server")]
    public string Server { get; set; }

    [XmlElement("timeOut")]
    public int TimeOut { get; set; }
}

Last edited Oct 12, 2011 at 8:28 PM by drblame, version 2

Comments

No comments yet.