…meie igapäevast IT’d anna meile igapäev…

2012-04-05

Generic methods to get configuration values

Filed under: Programmeerimine — Sander @ 11:35:35
Tags: ,

CSharpLogoFor some reason, there are things in code that I just cannot remember. One of these things is getting values from configuration – web.config or app.config.

Yes, I know, it is ridiculous. I remember literally hundreds of classes and methods, but a simple, one-line “ConfigurationManager.AppSettings[key]” I have to google. So I just gave up and wrote a wrapper – and to make it a bit more useful, I did a generic version, which can get the configuration value already in a specified type.

So, here is the three-method class, imaginatively named “Config”:

  1. /// <summary>
  2. /// Shorthand class for easy access to web.config values
  3. /// </summary>
  4. public static class Config
  5. {
  6.  
  7.     /// <summary>
  8.     /// Gets the setting as string by key.
  9.     /// Return string.Empty if the key does not exist
  10.     /// </summary>
  11.     /// <param name="key">The key.</param>
  12.     /// <returns></returns>
  13.     public static string GetSettingByKey(string key)
  14.     {
  15.         return ConfigurationManager.AppSettings[key] ?? string.Empty;
  16.     }
  17.  
  18.     /// <summary>
  19.     /// Generic version of GetSettingByKey().
  20.     /// Returns default(T) if the key does not exist
  21.     /// </summary>
  22.     /// <typeparam name="T"></typeparam>
  23.     /// <param name="key">The key.</param>
  24.     /// <returns></returns>
  25.     public static T GetSettingByKey<T>(string key)
  26.     {
  27.         var value = ConfigurationManager.AppSettings[key];
  28.  
  29.         return string.IsNullOrWhiteSpace(value) ? default(T) : (T)(object)(value);
  30.     }
  31.  
  32.     /// <summary>
  33.     /// Returns defaultValue, if the key does not exist.
  34.     /// </summary>
  35.     /// <typeparam name="T"></typeparam>
  36.     /// <param name="key">The key.</param>
  37.     /// <param name="defaultValue">The default value.</param>
  38.     /// <returns></returns>
  39.     public static T GetSettingByKey<T>(string key, T defaultValue)
  40.     {
  41.         var value = ConfigurationManager.AppSettings[key];
  42.  
  43.         return string.IsNullOrWhiteSpace(value) ? defaultValue : (T)(object)(value);
  44.     }
  45. }

Use it simply Config.GetSettingByKey<int>(“RowCount”) – which would return 0, if the key “RowCount” doesn’t exist. Use Config.GetSettingByKey<int>(“RowCount”, 16) to specify a different default value.

You could also do separate properties for more frequently used configuration values, to avoid typos in string keys:

  1. public static DateTime MyDateTime
  2. {
  3.     get { return GetSettingByKey<DateTime>("MyDateTime", DateTime.Now); }
  4. }

About these ads

Lisa kommentaar »

Kommentaare veel pole.

Selle postituse kommentaaride RSS-voog. TrackBack URI

Lisa kommentaar

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Muuda )

Twitter picture

You are commenting using your Twitter account. Log Out / Muuda )

Facebook photo

You are commenting using your Facebook account. Log Out / Muuda )

Google+ photo

You are commenting using your Google+ account. Log Out / Muuda )

Connecting to %s

Theme: Rubric. Get a free blog at WordPress.com

Follow

Get every new post delivered to your Inbox.

Join 83 other followers