Application Settings

Yesterday, I wrote about wrapping your session state variables into a class. The same thing can be done with other variables such as application settings and connection strings. For this class I normally name the class MySettings.

The code for doing this is very similar to yesterdays code.

public static string mailFrom
{
    get { return ConfigurationManager.AppSettings["MailFrom"].ToString(); }
}
public static string calendarConnectionString
{
    get { return ConfigurationManager.ConnectionStrings["Test"].ToString(); }
}

As with yesterdays examples, it is then very easy to use these applications settings. When you want to use these settings you’d write one of the following:

MySettings.calendarConnectionString
MySettings.mailFrom

Again, this extra class will add a few extra lines of code to your project. However, in the long run it will save you time and effort when you later decide to change something. For example, “Test” is not a good name to call your connection string, so you may want to change it to “CalendarDatabase”. Using this technique, you would change it in your web.config file and in this class, and you’re done. Without this class, you would need to find and replace in your entire source code for the application. What happens when you miss one?

Another advantage of implementing your application settings this way is that Visual Studio’s IntelliSense works with it. I really like IntelliSense!

Advertisements

1 comment so far

  1. Darin Dugan on

    IntelliSense is definitely one of the best features of Visual Studio, but don’t forget the great side-effect: tab completion!


Comments are closed.

%d bloggers like this: