.NET Custom Settings

We normally tend to lazy and use the .NET Configuration AppSettings element for defining our own custom configuration keys. Though this does solve the purpose but sometime we need MORE. We may need our own tags like <myTag> to be used in the configuration file. .NET provides a standard mechansim to achieve this. Lets assume we want the following in our .config file

<MySettings>

    <Setting Name=”” Value=”” IsValid=”” />

</MySettings>

There are 5 things to be done

  1. Define a class that inherits from ConfigurationElement, this represent the element node i.e. <MySetting> for the above mentioned XML.
  2. Define a class that inherits from ConfigurationElementCollection, this represents the collection node that contain the element node i.e. <MySettings> for the above mentioned XML.
  3. Define a class that inherits from ConfigurationSection, this represent the master class through which the section is accessed.
  4. Add entry in the <configSections> of the config file.
  5. Use System.Configuration.ConfigurationManager.GetSection method to access the section defined in the code.

Now let’s drill with the code

Inherit from ConfigurationElement

public class MySetting : ConfigurationElement
    {
                
        [ConfigurationProperty(“Name”, IsRequired = true)]
        public String Name
        {
            get
            {
                return (string)this[“Name”];
            }
            set
            {
                this[“Name”] = value;
            }
        }

      
        [ConfigurationProperty(“Value”)]
        public String Value
        {
            get
            {
                return (string)this[“Value”];
            }
            set
            {
                this[“Value”] = value;
            }
        }

        [ConfigurationProperty(“IsValid”)]
        public bool IsValid
        {
            get
            {
                return (bool)this[“IsValid”];
            }
            set
            {
                this[“IsValid”] = value;
            }
        }
    }

Inherit from ConfigurationElementCollection

public class MySettings : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new MySetting();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((MySetting)element).Name;
        }
        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }
        protected override string ElementName
        {
            get { return “MySetting”; }
        }
        public MySetting this[int index]
        {
            get{ return (MySetting)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }
        new public MySetting this[string Name]
        {
            get { return (MySetting)BaseGet(Name); }
        }
        public bool ContainsKey(string key)
        {
            bool result = false;
            object[] keys = BaseGetAllKeys();
            foreach (object obj in keys)
            {
                if ((string)obj == key)
                {
                    result = true;
                    break;
                }
            }
            return result;
        }
    }

Inherit from ConfigurationSection

public class MySection:ConfigurationSection
    {
       [ConfigurationProperty(“MySettings”,IsDefaultCollection=true)]
        public MySettings MySetting
        {
            get
            {
                return (MySettings)base[“MySettings”];
            }
        }
    }

 

Entry in App.Config/Web.Config

<configuration>
  <configSections>
    <section name=”MySettings” type=”DebasishPramanik.Configuration.MySection,
      CustomSettings”/>
  </configSections>
  <MySettings>
    <MySetting Name=”BlogID” Value=”12345″ IsValid=”true”/>
  </MySettings>
</configuration>

 

Use in Application

 

DebasishPramanik.Configuration.MySection section = (DebasishPramanik.Configuration.MySection)System.Configuration.ConfigurationManager.GetSection("MySettings");
            
foreach (DebasishPramanik.Configuration.Setting setting in section.MySettings)
 {
     Console.WriteLine(setting.Name);
}

 

 

    Advertisements

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

    %d bloggers like this: