Created
April 4, 2014 15:12
-
-
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()
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
| 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