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:
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.