Created
February 3, 2015 11:54
-
-
Save jrgcubano/3ff78fdf50a728caf57b to your computer and use it in GitHub Desktop.
Snippet to get AppSettings from ConfigurationManager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SNIPPETS: GET APPSETTINGS FROM XXX.CONFIG IN A GENERIC WAY | |
This snippet can be used in all web projects where there is a need to get settings from the AppSettings section in web.config. | |
INITIAL SNIPPET: | |
? | |
public static T GetAppSetting<T>(string key, T defaultValue) | |
{ | |
if (!string.IsNullOrEmpty(key)) | |
{ | |
string value = ConfigurationManager.AppSettings[key]; | |
try | |
{ | |
if (value != null) | |
{ | |
var theType = typeof(T); | |
if (theType.IsEnum) | |
return (T)Enum.Parse(theType, value.ToString(), true); | |
return (T)Convert.ChangeType(value, theType); | |
} | |
return default(T); | |
} | |
catch { } | |
} | |
return defaultValue; | |
} | |
This method (static or not) makes it easy and clean to get settings converted to the type needed. Also it enables default values to be fetched if the setting doesn't exist or can be converted to the specified type. | |
It can be used in different ways, like so; | |
<!-- Imagine we have these settings: --> | |
<appSettings> | |
<add key="Title" value="Web title" /> | |
<add key="VersionNumber" value="5" /> | |
<add key="UseThemes" value="True" /> | |
<add key="WebTheme" value="Light" /> | |
<add key="WebThemeNumber" value="1" /> | |
</appSettings> | |
Initially we could call it like this: | |
string title = GetAppSetting<string>("Title", "My title"); | |
But even better, we could let the type be inferred from the second parameter and call it like this: | |
string title = GetAppSetting("Title", "My title"); | |
int version = GetAppSetting("VersionNumber", 123); | |
bool useThemes = GetAppSetting("UseThemes", false); | |
We could also use enums. Imagine we have this enum: | |
// Imagine this enum: | |
enum MyThemes | |
{ | |
Light, | |
Dark, | |
Office | |
} | |
Now just get the setting as done before: | |
MyThemes theme = GetAppSetting("WebTheme", MyThemes.Office); | |
// Both enum name and index will work. | |
// (Remember the WebThemeNumber was number 1, so it will return MyThemes.Dark.) | |
theme = GetAppSetting("WebThemeNumber", MyThemes.Office); | |
EXTENDED SNIPPET: DEFERRED DEFAULT VALUE | |
One option we can do with the default value, if we want deferred execution on it, is by making an overload of the method and change the defaultValue parameter type to a Func<T>. This would be handy when the default value might have some logic that's heavy to execute. | |
Overload example: | |
public static T GetAppSetting<T>(string key, Func<T> defaultValue) | |
{ | |
if (!string.IsNullOrEmpty(key)) | |
{ | |
string value = ConfigurationManager.AppSettings[key]; | |
try | |
{ | |
if (value != null) | |
{ | |
var theType = typeof(T); | |
if (theType.IsEnum) | |
return (T)Enum.Parse(theType, value.ToString(), true); | |
return (T)Convert.ChangeType(value, theType); | |
} | |
return default(T); | |
} | |
catch { } | |
} | |
return defaultValue(); | |
} | |
public static T GetAppSetting(string key, T defaultValue) | |
{ | |
return GetAppSetting(key, () => defaultValue); | |
} | |
Then it would be used like so: | |
var value = GetAppSetting("MySetting", () => MyClass.DoSomethingTimeConsuming()); | |
// Or with an anonymous method | |
var value = GetAppSetting("MySetting", () => | |
{ | |
var builder = new StringBuilder(); | |
// Do heavy stuff here. | |
return builder.ToString(); | |
}); | |
This way the heavy stuff would only be executed if the default value is needed. | |
EXTENDED SNIPPET: THROW EXCEPTION | |
This snippet could also be extended with an overload for throwing exceptions, like so: | |
public static T GetAppSetting<T>(string key, T defaultValue, bool throwExceptions) | |
{ | |
if (!string.IsNullOrEmpty(key)) | |
{ | |
string value = ConfigurationManager.AppSettings[key]; | |
try | |
{ | |
if (value != null) | |
{ | |
var theType = typeof(T); | |
if (theType.IsEnum) | |
return (T)Enum.Parse(theType, value.ToString(), true); | |
return (T)Convert.ChangeType(value, theType); | |
} | |
return default(T); | |
} | |
catch | |
{ | |
if (throwExceptions) | |
throw; | |
} | |
} | |
return defaultValue(); | |
} | |
public static T GetAppSetting(string key, T defaultValue) | |
{ | |
return GetAppSetting(key, defaultValue, false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment