Tuesday, February 02, 2010

Take Control of Your Web.Config




After reading a Paul Sheriff article in CODE Magazine, I ended up creating a base WebConfig class to handle getting values out of my web.config. I’ve hated the ConfigurationManager.AppSettings since they deprecated it from ConfigurationSettings.AppSettings. So now I have my own GetAppSetting so I never have to worry about it again. 

My base class looks like this:

public class BaseWebConfig
{
protected static string GetAppSetting(string appSettingName)
{
if (ConfigurationManager.AppSettings[appSettingName] == null)
throw new NullReferenceException(appSettingName
+
" AppSettings value cannot be null or empty in the web.config.");

return ConfigurationManager.AppSettings[appSettingName];
}

protected static bool GetBoolAppSetting(string appSettingName)
{
return Convert.ToBoolean(GetAppSetting(appSettingName));
}

protected static string[] GetStringArraySetting(string appSettingName)
{
var values = GetAppSetting(appSettingName).Split(new[] { ',' });
return values;
}

protected static int GetIntAppSetting(string appSettingName)
{
int i;
if (!int.TryParse(GetAppSetting(appSettingName), out i))
throw new InvalidCastException(appSettingName
+
" AppSettings value is an invalid integer.");

return i;
}

protected static DateTime GetDateTimeAppSetting(string appSettingName)
{
DateTime dt;
if (!DateTime.TryParse(GetAppSetting(appSettingName), out dt))
throw new InvalidCastException(appSettingName
+
" AppSettings value is an invalid DateTime.");

return dt;
}

protected static string GetConnectionString(string connectionStringName)
{
if (ConfigurationManager.ConnectionStrings[connectionStringName] == null)
throw new NullReferenceException(connectionStringName
+
" ConnectionStrings value cannot be null in the web.config.");

if (String.IsNullOrEmpty(ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString))
throw new NullReferenceException(connectionStringName
+
" ConnectionStrings value cannot be null or empty in the web.config.");

return ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
}
}

Here’s the breakdown:



  • GetAppSetting – Gets a string value

  • GetBoolAppSetting – Gets a bool value

  • GetStringArraySetting – Gets a string[] value

  • GetIntAppSetting – Gets an int value

  • GetDateTimeAppSetting – Gets a DateTime value

  • GetConnectionString – Gets a connection string value

Pretty simple stuff here…well I think it’s self-explanatory. Note they’re all protected so the inheriting class can access them, but nothing else. The reason I do this is because I never want a magic string passed in and risk getting a run-time error with a simple typo.


So with this base class, I inherit it in another WebConfig class in my actual project. Here’s an example of one:

public class WebConfig : BaseWebConfig
{
public static string ReturnEmail
{
get { return GetAppSetting("ReturnEmail"); }
}

public static bool IsTestEnvironment
{
get { return GetBoolAppSetting("IsTestEnvironment"); }
}

public static DateTime ShutOffDate
{
get { return GetDateTimeAppSetting("ShutOffDate"); }
}

public static string DbConnectionString
{
get { return GetConnectionString("DbConnectionString"); }
}
}

You can see above, I can reference my web.config all day long and never worry about mistyping something. If I need to rename something, I can easily do that as well. If you’re a reader of my blog, you know I’m a huge fan of being strongly-typed. I love for intellisense to help me out :)


I really like the simplicity of this method because it’s easy for beginner team members to use. That’s all I got…let me know if you have comments.


Thanks for reading!


Shout it

kick it on DotNetKicks.com

blog comments powered by Disqus
Related Posts Plugin for WordPress, Blogger...