web 2.0

Stored Provider Based Configuration Management Implementation

Hi,

In this article I’ll talk about a different way to store Configuration Values instead of using App.config or Web.config.

App.config provide a simple way to quickly store Key/Values but when we’re working with a large Application with lot of Settings it could be interesting to store theses Configuration Keys into a Database in order to be able to change it quickly and so without need to restart our Application.

The Stored Configuration Management Implementation is described by the following Diagram

image

StoredConfigurationProviderBase Implementation

public abstract class StoredConfigurationProviderBase : ProviderBase, IStoredConfigurationProviderBase
{
    #region Public Abstract Methods

    public abstract String GetValue(String Key);

    #endregion
}

 

This set of classes is the base of the Configuration to get our Stored Configuration Management working later.

Implement a Custom Stored Configuration Provider

To implement a Custom Stored Configuration Provider we need to inherits a class from StoredConfigurationProviderBase abstract class.

public class MyConfigProvider : StoredConfigurationProviderBase
{
    public override string GetValue(string Key)
    {
        // GET FROM DATABASE (Using LinqToSql, ADO ....)
    }
}

 

Setup our Custom Provider into App.config

<configuration>
  <configSections>
    <section name="StoredConfiguration" type="Sb2.Core.Configuration.StoredConfigurationSection,Sb2.Core" />
  </configSections>
  <StoredConfiguration defaultProvider="Default">
    <providers>
      <add name="Default" type="Sb2.Core.Tests.Configuration.MyConfigProvider,Sb2.Core.Tests" />
    </providers>
  </StoredConfiguration>
</configuration>

 

Now we got our Custom Stored Configuration Provider working, So We are going to Use It !

The Stored Configuration Elements are described by the following Diagram

image

Implement a Custom Configuration Element

In order to implement a Custom Configuration Element, we just need to inherits a class from ConfigurationBase abstract class.

public class MyConfigElement : ConfigurationBase
{
    #region Constructors
    
    public MyConfigElement()
        : base("MyConfigElement")
    {
    } 

    #endregion

    public Boolean MyBoolKey
    {
        get
        {
            return base.GetBoolValue("MyBoolKey", false);
        }
    }
    public String MyStringKey
    {
        get
        {
            return base.GetStringValue("MyStringKey", String.Empty);
        }
    }
}

Database Sample Table

image

How-to Use It ?

static void Main(string[] args)
{
    Boolean MyBoolValue = StoredConfigurationManager<MyConfigElement>.Instance.MyBoolKey;
    Console.WriteLine(MyBoolValue);

    String MyStringValue = StoredConfigurationManager<MyConfigElement>.Instance.MyStringKey;
    Console.WriteLine(MyStringValue);
}

 

Now we have a pretty simple usable Stored Configuration Elements Management.

You can download the following Sample Solution.

Download Solution - StoredConfiguration.zip

Hope this Help’s!



Views(1001)

kick it on DotNetKicks.com

Share/Save/Bookmark Subscribe

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C#

Comments

Add comment


 

biuquote
Loading



Technorati Profile