Skip to content

Instantly share code, notes, and snippets.

@joshgo
Created April 4, 2014 15:12
Show Gist options
  • Save joshgo/9976724 to your computer and use it in GitHub Desktop.
Save joshgo/9976724 to your computer and use it in GitHub Desktop.
ConfigSettings class for accessing the config file ... a nice clean way. It provides a Require()
public class ConfigSettings : System.Dynamic.DynamicObject
{
private static readonly System.Lazy<ConfigSettings> _settings = new Lazy<ConfigSettings>(() => new ConfigSettings());
private ConfigSettings()
{ }
public static dynamic Instance { get { return _settings.Value; } }
public override bool TryGetMember(System.Dynamic.GetMemberBinder binder, out object result)
{
result = ConfigurationManager.AppSettings[binder.Name];
return true;
}
public static string Require(string key)
{
if (ConfigurationManager.AppSettings[key] == null)
throw new ApplicationException(string.Format("Config File Key: {0} not found", key));
return ConfigurationManager.AppSettings[key];
}
public static string Optional(string key, string defaultValue)
{
return ConfigurationManager.AppSettings[key] ?? defaultValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment