DCSIMG
Configuration shortcuts (or IntelliSense for configuration file) - Itai Goldstein

Configuration shortcuts (or IntelliSense for configuration file)

The configuration appSettings & connectionStrings sections are highly used in our daily application development.

<appSettings> <add key="ApplicationName" value="ConfigurationShortcutsSample"/> </appSettings>

The way we usually access the values which are stored within these sections is through the ConfigurationManager class when we specify the section key string:

string applicationName = ConfigurationManager.AppSettings [AppSettingsKeys.ApplicationName];

This methodology could lead us developers to mistakes which can be detected only at runtime in case we mistyped the application setting key string.

I would like to offer another way of accessing these sections which will prevent us from mistyping the application setting key and even provide sort of IntelliSense.

In this way, we need to write a static class which will hold the keys for our configuration section keys in static properties:

public static class AppSettingsKeys { public static string ApplicationName { get { return "ApplicationName"; } } }

That’s it.. now we can access our configuration application settings keys with the help of our static class:

image

Summary

In this post I reviewed a way to have shortcuts to our configuration file appSettings & connectionStrings sections which can act as IntelliSense and prevent us from coding mistakes which can be detected only on runtime.

Hope you’ll find it useful.

Published Wednesday, February 18, 2009 10:39 AM by itai
תגים:, , ,

Comments

# Configuration shortcuts (or IntelliSense for configuration file)

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Wednesday, February 18, 2009 11:18 AM by DotNetKicks.com

# Configuration Shortcuts

In replay to Itay’s post on configuration shortcuts, first and foremost is in some situations you can

Sunday, February 22, 2009 3:05 PM by dlc about.net (and stuff)

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: