Configuration shortcuts (or IntelliSense for configuration file)

18 בפברואר 2009

תגיות: , , ,
אין תגובות

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.

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים