Organize a C# project to customize it from outside of source code

1 Apr

For any project we feel to process some configuration variables so that we can customize the software from out of sources code. It is important to make those variables available in each part of the project minimizing the system load. We can put the values either in an xml or Database. In my last C# project I’ve followed an interesting process.

I used a Single Tone Pattern Class to process my variables and declare the constructor “private” so that we can’t create more than one instance with “new” keyword.

private Dictionary sconfig;

Dictionary is a fantastic element to store temporary data. Here I used it something like associative array.

private static BCReaderConfiguration _bcrConfigObj = null;
public static BCReaderConfiguration getObject()
{
if (_bcrConfigObj == null)
{
_bcrConfigObj = new BCReaderConfiguration();
_bcrConfigObj.setConfig();
return _bcrConfigObj;
}
else
{
return _bcrConfigObj;
}
}

This the most interesting part of my coding. getObject() is a static method so that I can call it from anywhere with the name of class. Like:

BCReaderConfiguration.getObject();

I have declared a private variable with the type of my configuration class (“BCReaderConfiguration”) and storing the first instance of this object. Each time I call the function form other part of my coding first I check if the object already created. If this object is not created (First time call) then it’s creating new object otherwise returning the previous one. This process will save my Physical Memory as well as time because we are using same object for thousands of calls.

Let’s consider an XML file where all configuration variables are saved. Here is the example XML file :

<sconfig>
<node><svalue soption="check_update_title">Wait Checking for update</svalue> </node> <node><svalue soption="bad_database_structure">Bad Database structure!</svalue> </node> <node><svalue soption="guest_success_msg">Welcome!</svalue> </node>
<node><svalue soption="guest_failed_msg">Sorry</svalue> </node>
<node><svalue soption="general_inconvenience_msg">Sorry! For inconvenience</svalue> </node> <node><svalue soption="about">Version 1.0</svalue></node>
</sconfig>

Each “node” tag preserving one configuration value inside the tag and attribute “svalue soption” containing the variable name.

private XmlDocument getConfigXML ()
{
XmlDocument document = new XmlDocument();
return document.Load(“xmls\\config.xml”);
}

getConfigXML() is a simple function to read the xml and return the Xml type object.

private void setConfig()
{
this.sconfig = new Dictionary();
// Collect the xml data

XmlDocument sconfigXmlDoc = this.getConfigXML ();

XmlNodeList list = null;
list = sconfigXmlDoc.GetElementsByTagName(“node”);

foreach (XmlNode val in list)
{
this.sconfig[val[“svalue”].GetAttribute(“soption”)] = val[“svalue”].InnerText;
}
}

This is a pretty easy section. Here I am populating all my data in a 2D Dictionary. XmlNodeList is a C# data type to process Xml node from XmlObject.

Well, we are all most done. Now we need a Public function that can return our desired configuration variable.

public String get_config(String soption)
{
try
{
return this.sconfig[soption];
}
catch
{
return “”;
}
}

I used “get_config()” method that accept a Configuration variable name as parameter and return the desired value and in case of any exception it will return an empty string.

Finally, to get any configuration variable we will call something like this:

BCReaderConfiguration.getObject().get_config(“guest_success_msg”)

Full source Code here:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
namespace barcodereader
{
class BCReaderConfiguration
{
private Dictionary sconfig;
private static BCReaderConfiguration _bcrConfigObj = null;

private BCReaderConfiguration()
{
}

// Single Tone object generator
public static BCReaderConfiguration getObject()
{
if (_bcrConfigObj == null)
{
_bcrConfigObj = new BCReaderConfiguration();
_bcrConfigObj.setConfig();
return _bcrConfigObj;
}
else
{
return _bcrConfigObj;
}
}

private XmlDocument getConfigXML ()
{
XmlDocument document = new XmlDocument();
return document.Load(“config.xml);
}

// Populate the configuration in to array
private void setConfig()
{
this.sconfig = new Dictionary();
XmlDocument sconfigXmlDoc = this.getConfigXML();
XmlNodeList list = null;
list = sconfigXmlDoc.GetElementsByTagName(“node”);

foreach (XmlNode val in list)
{
this.sconfig[val[“svalue”].GetAttribute(“soption”)] = val[“svalue”].InnerText;
}
}

// Return the Configuration value
public String get_config(String soption)
{
try
{
return this.sconfig[soption];
}
catch
{
return “”;
}
}
}
}

Advertisements

5 Responses to “Organize a C# project to customize it from outside of source code”

  1. ahsan April 1, 2009 at 11:09 am #

    great use of singleton pattern.

  2. Rubel April 1, 2009 at 11:14 am #

    Yes. It’s useful in case of frequently declaration of an object.

  3. Saurav April 1, 2009 at 6:05 pm #

    Now u r in .NET Platform ..

    Whts Next …
    A. Java
    B. C++
    C. C
    D. Your Own Language ,

    I think it should be D..

  4. Rubel April 1, 2009 at 6:13 pm #

    😉

  5. Rumana April 2, 2009 at 4:39 am #

    gr8 job. I like your structured working process.

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: