Club Manager : ConfigurationManager

MemberShip Plan Manager

Now we need to persist and retrieve data from database but we have not configured the same. Let us do that next.

We will soon come to know that we will be requiring a lot of configuration values for Database(Host, Port, UserName, Password, DBName), Application(Title) and so on and if we store all the configuration in our app.config it will make our application quite dirty, so we decide to keep a key in our app.config which will refer to a xml file from where we can read our configuration values. Let us create a framework for us to deal with the same.

We read Config xml in a class and hold all the nodes over there, other ask for their node and process them as required. If none of the configuration is required why read the file at all, so we will use static constructors to lazy read the configuration.

Add a new class library project named Core.Configuration, delete Class1.cs from the project and add a reference to System.Configuration. Once done add a new class file ConfigurationManager.cs.
We define AppConfig as our required configuration xml key and store configuration xml in Dictionary<string, XElement> object named ConfigValue.
We iterate the document and add each node under root to the configuration value and we are done. A really very short code to do exactly what we need, shared below:

using System.Collections.Generic;
using System.Xml.Linq;
 
namespace Core.Configuration
{
    /// <summary>
    /// Configuration Manager to retrieve XML Configurations
    /// </summary>
    public class ConfigurationManager
    {
        #region Configuration Values
 
        /// <summary>
        /// XML Configuration nodes mapped against node key
        /// </summary>
        public static Dictionary<string, XElement> ConfigValue = new Dictionary<string, XElement>();
 
        #endregion Configuration Values
 
        #region XML Configuration Keys
 
        /// <summary>
        /// Key to be searched in App.Config/Web.Config
        /// </summary>
        private static string APP_CONFIG = "AppConfig";
 
        #endregion XML Configuration Keys
 
        #region Constructors
 
        /// <summary>
        /// Static Initializer which is invoked automatically as required
        /// </summary>
        static ConfigurationManager()
        {
            XDocument XDoc = XDocument.Load(System.Configuration.ConfigurationManager.AppSettings[APP_CONFIG]);
 
            foreach (XElement ele in XDoc.Root.Elements())
                ConfigValue.Add(ele.Name.LocalName, ele);
        }
 
        #endregion Constructors
    }
}

Browse this commit on Github

Add App.config(Add->New Item->Application Configuration Project) to main project ClubManager and add a key “AppConfig” with a value “Config.xml“.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="AppConfig" value="Config.xml"/>
  </appSettings>
</configuration>

Next we need to add Config.xml file in the project (Add->New Item->XML File) We will use Club as our Root Node so add the node to file and save it.

<?xml version="1.0" encoding="utf-8" ?>
<Club>
</Club>

We want this Config.xml to be available to us at runtime so we change the “Copy To Output” property of file to “Copy if newer“. Build the project and you will find Config.xml in your bin/Debug directory.

Browse this commit on Github

Leave a Reply