The Darkside

Shedding light on things and stuff

 
  Home :: Contact :: Syndication  :: Login
  75 Posts :: 0 Stories :: 49 Comments :: 2 Trackbacks

Ads

Archives

Post Categories

Open Source Projects

Other Blogs

Adding your own configuration values into a config file is really simplified when it comes to using the appsettings section, but do you really need all those key-value pairs clogging up your file and not having any noticable relation to one other? Making use of a custom configuration section allows you to group the settings you want related to each other and even add validation to the key-value pairs.

 

The code below (this is the entire class) is for setting up your own configuration section. I've included three different properties of varying types, and also made use of configuration validation attributes to validate the data that may come from the config file.

The [ConfigurationProperty] attribute is used to map the property in code to the key/value pair in the config section.  

using System;

using System.Configuration;

 

namespace Darkside.Configuration

{

    public class CustomConfigSection : ConfigurationSection

    {

 

        [ConfigurationProperty("AllowMassiveDiscounts", IsRequired = true, DefaultValue = false)]

        public bool AllowMassiveDiscounts

        {

            get { return (bool)this["AllowMassiveDiscounts"]; }

            set { this["AllowMassiveDiscounts"] = value; }

        }

 

        [ConfigurationProperty("MaximumDiscountValue", IsRequired = false, DefaultValue = 0)]

        [IntegerValidator(MinValue=0, MaxValue=90)]

        public int MaximumDiscountValue

        {

            get { return Convert.ToInt32(this["MaximumDiscountValue"]); }

            set { this["MaximumDiscountValue"] = value; }

        }

 

        [ConfigurationProperty("FestiveSeasonGreeting", IsRequired = false, DefaultValue = false)]

        [StringValidator (MaxLength=50, InvalidCharacters="<>#$%")]

        public bool FestiveSeasonGreeting

        {

            get { return (bool)this["FestiveSeasonGreeting"]; }

            set { this["FestiveSeasonGreeting"] = value; }

        }

 

    }

}

 Adding the cusom section into your configuration file requires two steps, the first indicating that you have introduced a custom section, and the second the custom section itself.

 The follow needs to be added to the configSections area. (Just a note: always remeber that the configSections element must be first in the config file).

The type parameter on the sectionis type="(Fully Qualified Name of Class), (Name of Assembly)"

<configSections>

    <section name="CustomConfigSection" type="Darkside.Configuration.CustomConfigSection, Darkside.Configuration" />

</configSections>     

...and the next section (your custom section) can go anywhere in the config file, on the same level as the configSections element (i.e. First child)

<CustomConfigSection

    AllowMassiveDiscounts="false"

    MaximumDiscountValue="80"

    FestiveSeasonGreeting="Have a wondeful festive season!" 

/>   

 That's it.

posted on Friday, November 09, 2007 7:02 AM
Comments have been closed on this topic.