KnowDotNet

Use Application Wrapper Class To Access Web.Config Values

Put Configuration.Manager References in Application Wrapper Class

by Les Smith

Use an Application Wrapper Class to place accessing of all configuration parameters in one place that the whole web application can reference so that you always know where to go to get configuration settings.  This will keep developers from wondering where to find all app settings and not to have to do all that is involved with retrieving values from the Web.Config, or for that matter in an app.config file.

The following code is an excerpt from a real ApplicationWrapper Class just to show an example of the whole class.  This example is obviously from C#, but you can use ReadOnly Properties in VB.NET just as well.  Make sure you have a Reference to the System.Configuration.DLL.  Don't you hate it when someone posts code without telling you the refereneces and namespaces needed to make it work?  I know I have done it myself, but it is always helpful if example code compiles...

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;

public static class ApplicationWrapper
{
    #region public members
    
public static string DBConnection
    {
        
get { return ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString; }
    }
    
public static int MaxLoginAttempts
    {
        
get { return int.Parse(ConfigurationManager.AppSettings["MaxLoginAttempts"]); }
    }
    
public static string Company
    {
        
get { return ConfigurationManager.AppSettings["Company"]; }
    }
    #endregion
}

This only shows three properties out of dozens used in the Web Application that it was pulled from, but you get the idea.  This kind of convention keeps everybody working on the application on the same page, no pun intended.

Need to automatically organize your code windows?  You'll be amazed how easy it is to keep the code in your code windows organized.  TRY IT FREE FOR 30 DAYS BY CLICKING HERE.



Automatically generate braces in C#! Try CSharpCompleter and stop wasting valuable time needlessly typing hundreds of braces {} daily.  Try CSharpCompleter for 30 DAYS FREE.



Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog.
  

You can also email me directly at les@KnowDotNet.com.