创建BaseConfig轻松获取AppSetting

       我们经常存储一些配制信息在AppSettings中,是不是经常要写取它们值的代码。让我们写一个简单的基类吧:

   1:     /// <summary>
   2:      /// BaseWebConfig
   3:      /// </summary>
   4:      public class BaseWebConfig
   5:      {
   6:          #region Methods (6) 
   7:   
   8:          // Protected Methods (6) 
   9:   
  10:          /// <summary>
  11:          /// Gets the app setting.
  12:          /// </summary>
  13:          /// <param name="appSettingName">Name of the app setting.</param>
  14:          /// <returns></returns>
  15:          protected static string GetAppSetting(string appSettingName)
  16:          {
  17:              if (ConfigurationManager.AppSettings[appSettingName] == null)
  18:                  throw new NullReferenceException(appSettingName +
  19:                                                   " AppSettings value cannot be null or empty in the web.config.");
  20:              return ConfigurationManager.AppSettings[appSettingName];
  21:          }
  22:   
  23:          /// <summary>
  24:          /// Gets the bool app setting.
  25:          /// </summary>
  26:          /// <param name="appSettingName">Name of the app setting.</param>
  27:          /// <returns></returns>
  28:          protected static bool GetBoolAppSetting(string appSettingName)
  29:          {
  30:              return Convert.ToBoolean(GetAppSetting(appSettingName));
  31:          }
  32:   
  33:          /// <summary>
  34:          /// Gets the connection string.
  35:          /// </summary>
  36:          /// <param name="connectionStringName">Name of the connection string.</param>
  37:          /// <returns></returns>
  38:          protected static string GetConnectionString(string connectionStringName)
  39:          {
  40:              if (ConfigurationManager.ConnectionStrings[connectionStringName] == null)
  41:                  throw new NullReferenceException(connectionStringName +
  42:                                                   " ConnectionStrings value cannot be null in the web.config.");
  43:              if (String.IsNullOrEmpty(ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString))
  44:                  throw new NullReferenceException(connectionStringName +
  45:                                                   " ConnectionStrings value cannot be null or empty in the web.config.");
  46:              return ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
  47:          }
  48:   
  49:          /// <summary>
  50:          /// Gets the date time app setting.
  51:          /// </summary>
  52:          /// <param name="appSettingName">Name of the app setting.</param>
  53:          /// <returns></returns>
  54:          protected static DateTime GetDateTimeAppSetting(string appSettingName)
  55:          {
  56:              DateTime dt;
  57:              if (!DateTime.TryParse(GetAppSetting(appSettingName), out dt))
  58:                  throw new InvalidCastException(appSettingName + " AppSettings value is an invalid DateTime.");
  59:              return dt;
  60:          }
  61:   
  62:          /// <summary>
  63:          /// Gets the int app setting.
  64:          /// </summary>
  65:          /// <param name="appSettingName">Name of the app setting.</param>
  66:          /// <returns></returns>
  67:          protected static int GetIntAppSetting(string appSettingName)
  68:          {
  69:              int i;
  70:              if (!int.TryParse(GetAppSetting(appSettingName), out i))
  71:                  throw new InvalidCastException(appSettingName + " AppSettings value is an invalid integer.");
  72:              return i;
  73:          }
  74:   
  75:          /// <summary>
  76:          /// Gets the string array setting.
  77:          /// </summary>
  78:          /// <param name="appSettingName">Name of the app setting.</param>
  79:          /// <returns></returns>
  80:          protected static string[] GetStringArraySetting(string appSettingName)
  81:          {
  82:              string[] values = GetAppSetting(appSettingName).Split(new[] {','});
  83:              return values;
  84:          }
  85:   
  86:          #endregion Methods 
  87:      }


然后如何用呢?看下面的代码:

   1:      public class WebConfig1 : BaseWebConfig
   2:      {
   3:          public static string ReturnEmail
   4:          {
   5:              get { return GetAppSetting("ReturnEmail"); }
   6:          }
   7:   
   8:          public static bool IsTestEnvironment
   9:          {
  10:              get { return GetBoolAppSetting("IsTestEnvironment"); }
  11:          }
  12:   
  13:          public static DateTime ShutOffDate
  14:          {
  15:              get { return GetDateTimeAppSetting("ShutOffDate"); }
  16:          }
  17:   
  18:          public static string DbConnectionString
  19:          {
  20:              get { return GetConnectionString("DbConnectionString"); }
  21:          }
  22:      }

是不是很简单呀。这个代码你可以放到你自己的类库了,更重要是思想,重用的思想。DRY。Don’t repeat yourself.

希望这篇POST对您有帮助。

Author :Petter Liu      http://wintersun.cnblogs.com

原文地址:https://www.cnblogs.com/wintersun/p/1663769.html