Skip to content

Instantly share code, notes, and snippets.

@danieleli
Created November 1, 2011 02:03
Show Gist options
  • Save danieleli/1329649 to your computer and use it in GitHub Desktop.
Save danieleli/1329649 to your computer and use it in GitHub Desktop.
The world's great app setting retriever!
public class ApiSettings
{
public static ILog _logger = LogManager.GetLogger(typeof(ApiSettings));
// Use enums to prevent typos
public enum ConfigurationKeys
{
YesMailDomain,
YesMailUserName,
YesMailPassword,
YesMailUrl,
YesMailUseProxy
}
#region -- Constructors --
public ApiSettings()
: this(ConfigurationManager.AppSettings)
{
}
public ApiSettings(NameValueCollection appSettings)
{
// You can pass in settings for testing.
this.Url = ReadConfig(ConfigurationKeys.YesMailUrl, appSettings);
this.UserName = ReadConfig(ConfigurationKeys.YesMailUserName, appSettings);
this.Password = ReadConfig(ConfigurationKeys.YesMailPassword, appSettings);
this.Domain = ReadConfig(ConfigurationKeys.YesMailDomain, appSettings);
this.UseProxy = bool.Parse(appSettings[ConfigurationKeys.YesMailUseProxy.ToString()]);
if (this.UseProxy)
{
// ProxySettings is a nearly identical class but it's nested in this class
// So you can access it like so..
// var mySettings = new ApiSettings;
// var myProxyUserName = mySettings.ProxySettings.UserName;
this.ProxySettings = new ProxySettings(appSettings);
}
LogSettings(this);
}
// It throws KeyNotFoundException instead of the usually nullException
private static string ReadConfig(ConfigurationKeys key, NameValueCollection appSettings)
{
var value = "";
try
{
value = appSettings[key.ToString()];
}
catch (Exception)
{
// It even tells you what key was missing!
throw new KeyNotFoundException("App Settings Key not found for: " + key.ToString());
}
return value;
}
// Yup, does that too.
private void LogSettings(ApiSettings apiSettings)
{
var sb = new StringBuilder();
sb.Append("\n\nApiSettings\n-----------\n")
.Append("Url: ").Append(apiSettings.Url).Append("\n")
.Append("Domain: ").Append(apiSettings.Domain).Append("\n")
.Append("UserName: ").Append(apiSettings.UserName).Append("\n")
.Append("Password: ").Append(apiSettings.Password).Append("\n")
.Append("UseProxy: ").Append(apiSettings.UseProxy).Append("\n");
_logger.Debug(sb.ToString());
}
public ApiSettings(String domain, String userName, String password, String url, bool useProxy)
{
this.Url = url;
this.UserName = userName;
this.Password = password;
this.Domain = domain;
this.UseProxy = useProxy;
this.ProxySettings = new ProxySettings();
}
public ApiSettings(String domain, String userName, String password, String url, bool useProxy,
String proxyDomain, String proxyUserName, String proxyPassword, String proxyAddress, int proxyPort)
{
this.Url = url;
this.UserName = userName;
this.Password = password;
this.Domain = domain;
this.UseProxy = useProxy;
this.ProxySettings = new ProxySettings(proxyDomain, proxyUserName, proxyPassword, proxyAddress, proxyPort);
}
#endregion -- Constructors --
public string Url { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public bool UseProxy { get; set; }
public ProxySettings ProxySettings { get; set; }
public string Domain{ get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment