Created
July 17, 2014 11:10
-
-
Save trailmax/79fa78fdeed5b459d882 to your computer and use it in GitHub Desktop.
Configuration project
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
/// <summary> | |
/// Ambient Context for Configuration. Basically statically accessible configuration, | |
/// but we can override it. Use with caution, only in places where you can't inject through constructor | |
/// http://blogs.msdn.com/b/ploeh/archive/2007/07/23/ambientcontext.aspx | |
/// </summary> | |
public static class ConfigurationContext | |
{ | |
private static IConfiguration configuration; | |
public static IConfiguration Current | |
{ | |
get | |
{ | |
if (configuration == null) | |
{ | |
configuration = new MyConfiguration(); | |
} | |
return configuration; | |
} | |
set | |
{ | |
if (value == null) | |
{ | |
throw new NullReferenceException("configuration"); | |
} | |
configuration = value; | |
} | |
} | |
public static void ResetToDefault() | |
{ | |
configuration = new MyConfiguration(); | |
} | |
} |
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 interface IConfiguration | |
{ | |
string GetStorageConnectionString(); | |
string GetDatabaseConnectionString(); | |
string GetLogentriesToken(); | |
string GetContainerName(); | |
bool IsTestEnvironment(); | |
string GetSomeOtherSettingName(); | |
} |
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
//This is a configuration class that is used | |
// in production and reaches out to web.config | |
// it presumes all required settings are stored in appSettings section, including connection strings | |
// You can save connection strings to <connectionStrings> section of app config | |
// you'll need to use GetConnectionString private method | |
public class MyConfiguration : IConfiguration | |
{ | |
private readonly Dictionary<String, String> cachedSettings; | |
private readonly Object thisLock = new Object(); | |
public MyConfiguration() | |
{ | |
cachedSettings = new Dictionary<string, string>(); | |
} | |
public string GetStorageConnectionString() | |
{ | |
return GetSetting("StorageConnectionString"); | |
} | |
public string GetDatabaseConnectionString() | |
{ | |
return GetSetting("DatabaseConnectionString"); | |
} | |
public string GetLogentriesToken() | |
{ | |
return GetSetting("LOGENTRIES_TOKEN"); | |
} | |
public string GetContainerName() | |
{ | |
return GetSetting("ContainerName"); | |
} | |
public bool IsTestEnvironment() | |
{ | |
return string.Equals(GetSetting("IsTestEnvironment"), "true", StringComparison.InvariantCultureIgnoreCase); | |
} | |
public string GetSomeOtherSettingName() | |
{ | |
return GetSetting("SomeOtherSetting"); | |
} | |
// reaches ApplicationSettings section of web.config | |
private string GetSetting(string key) | |
{ | |
if (string.IsNullOrEmpty(key)) | |
{ | |
throw new ArgumentNullException("key"); | |
} | |
string cachedSetting; | |
if (cachedSettings.TryGetValue(key, out cachedSetting)) | |
{ | |
return cachedSetting; | |
} | |
// need thread safety if we are writing to the dictionary. | |
// Multiple simultaneous requests can try to write the same keys at the same time, causing an exception. | |
String result; | |
lock (thisLock) | |
{ | |
result = CloudConfigurationManager.GetSetting(key); | |
if (!cachedSettings.ContainsKey(key)) | |
{ | |
cachedSettings.Add(key, result); | |
} | |
} | |
return result; | |
} | |
// reaches <ConnectionStrings> section of web.config | |
private string GetConnectionString(String connectionStringName) | |
{ | |
if (string.IsNullOrEmpty(connectionStringName)) | |
{ | |
throw new ArgumentNullException("connectionStringName"); | |
} | |
string cachedSetting; | |
if (cachedSettings.TryGetValue(connectionStringName, out cachedSetting)) | |
{ | |
return cachedSetting; | |
} | |
// need thread safety if we are writing to the dictionary. | |
// Multiple simultaneous requests can try to write the same keys at the same time, causing an exception. | |
String result; | |
lock (thisLock) | |
{ | |
result = System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString; | |
if (!cachedSettings.ContainsKey(connectionStringName)) | |
{ | |
cachedSettings.Add(connectionStringName, result); | |
} | |
} | |
return result; | |
} | |
} |
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 StubConfiguration : IConfiguration | |
{ | |
// you can override databasde and storage connection strings | |
public string DatabaseConnectionString { get; set; } | |
public String StorageConnectionString { get; set; } | |
// if storage connection string is not overriden, | |
// use default - connect to Azure Storage emulator | |
public string GetStorageConnectionString() | |
{ | |
if (String.IsNullOrEmpty(StorageConnectionString)) | |
{ | |
return "UseDevelopmentStorage=true"; | |
} | |
return StorageConnectionString; | |
} | |
// if database connection string is not overriden | |
// check app.config for ApplicationSettings | |
// and if nothing is set there, use default one. | |
// app.setting can be transformed on build, and in build-server | |
// this can be overriden | |
public string GetDatabaseConnectionString() | |
{ | |
if (String.IsNullOrEmpty(DatabaseConnectionString)) | |
{ | |
var connectionString = ConfigurationManager.AppSettings["DatabaseConnectionString"]; | |
if (String.IsNullOrEmpty(connectionString)) | |
{ | |
connectionString = @"data source=.\SQLEXPRESS;integrated security=SSPI;Initial Catalog=MyApp_Testing;MultipleActiveResultSets=True"; | |
} | |
DatabaseConnectionString = connectionString; | |
} | |
return DatabaseConnectionString; | |
} | |
public string GetLogentriesToken() | |
{ | |
return ""; | |
} | |
public string GetContainerName() | |
{ | |
return "testupload"; | |
} | |
public string string GetSomeOtherSettingName() | |
{ | |
return "some other setting for testing" | |
} | |
public bool IsTestEnvironment() | |
{ | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment