Configuration Management with .NET Framework with ConfigurationManager

Configuration Management with .NET Framework with ConfigurationManager We do configuration management when we want to set up SQL server connection string, or selection version of the runtime engine your application suppose to use.

System.Configuration implements multiple interfaces that you can use in your development. Here is a list: IConfigurationSectionHandler – helps with accessing customized sections of the configuration file and all classes that implement this interface should be both thread safe and stateless, ISettingsProviderService, IApplicationSettingsProvider interfaces and ISettingsProviderService.

 

<configSections>

      <sectionGroupname="MyFirstSection"

         type="DBConnectionStringDemo.MyFirstSectionHandler,

      DBConnectionStringDemo"/>

</configSections>

<MyFirstSection>

      <DemoValues>

            <Value>

                  <Identifier>111</Identifier>

                  <SettingValue>System.Data.SqlClient</SettingValue>

            </Value>

            <Value>

                  <Identifier>112</Identifier>

                  <SettingValue>System.Data.OleDb</SettingValue>

            </Value>

        </MyFirstSection>
In order to work with the above specified configurations you need to employ IConfigurationSectionHandler

Hashtable ConfigValues = new Hashtable();
XmlElement Root = section as XmlElement;
String TempValue = string.Empty;
foreach (XmlNode ParentNode in Root.ChildNodes)
{
   foreach (XmlNode ChildNode in ParentNode.ChildNodes)
   {
      if (ChildNode.Name == "Identifier")
         {
         TempValue = ChildNode.InnerText;
         }
      if (ChildNode.Name == "SettingValue")
         {
         ConfigValues[TempValue] = ChildNode.InnerText;
         }
   }
}
ValuesHandler MyHandler = new ValuesHandler(ConfigValues);
return MyHandler;

Note that by using strong typing we can better verify the validity of values in a configuration files. In addition, any changes made to configuration file should be a stored with the help of Save Method.