Created
June 1, 2015 18:05
-
-
Save kenegozi/13f49f755ca4b1225631 to your computer and use it in GitHub Desktop.
Env based appSettings overrides, plus DictionaryAdapter fun
This file contains 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
using System; | |
using System.Configuration; | |
using System.Collections; | |
using Castle.Components.DictionaryAdapter; | |
namespace Demo { | |
class MainClass { | |
public static void Main (string[] args) { | |
var ageString = (string)ConfigurationManager.AppSettings ["Age"]; | |
var age = int.Parse (ageString); | |
Console.WriteLine ("My age is {0}", age); | |
var factory = new DictionaryAdapterFactory (); | |
var settings1 = factory.GetAdapter<ISomeSettings>(ConfigurationManager.AppSettings); | |
Console.WriteLine ("My age is {0}", settings1.Age); | |
var settings2 = factory.GetAdapter<ISomeSettings>(new EnvThenConfigSettings()); | |
Console.WriteLine ("My age is {0}", settings2.Age); | |
Environment.SetEnvironmentVariable ("CONFIG_Age", "120"); | |
Console.WriteLine ("My age is {0}", settings2.Age); | |
} | |
} | |
public interface ISomeSettings { | |
string Something { get; set; } | |
int Age { get; set; } | |
} | |
/// <summary> | |
/// Allow for Env based override to config's AppSettings. | |
/// It is a slimmed down IDictionary implementation. With this usage we only | |
/// care about the index getter, all the rest is no in use so throwing NIEs. | |
/// </summary> | |
public class EnvThenConfigSettings : IDictionary { | |
private const string ENV_PREFIX = "CONFIG_"; | |
public object this [object index] { | |
get { | |
var key = index as string; | |
if (key == null) { | |
throw new ArgumentException ("Only string keys are supported", "index"); | |
} | |
var vars = Environment.GetEnvironmentVariables (); | |
var envKeyToLookFor = ENV_PREFIX + key; | |
if (vars.Contains (envKeyToLookFor)) { | |
return Environment.GetEnvironmentVariable (ENV_PREFIX + index); | |
} | |
return ConfigurationManager.AppSettings [key]; | |
} | |
set { | |
throw new NotImplementedException (); | |
} | |
} | |
bool System.Collections.IDictionary.Contains (object key) { | |
throw new NotImplementedException (); | |
} | |
void System.Collections.IDictionary.Add (object key, object value) { | |
throw new NotImplementedException (); | |
} | |
void System.Collections.IDictionary.Clear () { | |
throw new NotImplementedException (); | |
} | |
System.Collections.IDictionaryEnumerator System.Collections.IDictionary.GetEnumerator () { | |
throw new NotImplementedException (); | |
} | |
void System.Collections.IDictionary.Remove (object key) { | |
throw new NotImplementedException (); | |
} | |
System.Collections.ICollection System.Collections.IDictionary.Keys { | |
get { | |
throw new NotImplementedException (); | |
} | |
} | |
System.Collections.ICollection System.Collections.IDictionary.Values { | |
get { | |
throw new NotImplementedException (); | |
} | |
} | |
bool System.Collections.IDictionary.IsReadOnly { | |
get { | |
throw new NotImplementedException (); | |
} | |
} | |
bool System.Collections.IDictionary.IsFixedSize { | |
get { | |
throw new NotImplementedException (); | |
} | |
} | |
void System.Collections.ICollection.CopyTo (Array array, int index) { | |
throw new NotImplementedException (); | |
} | |
int System.Collections.ICollection.Count { | |
get { | |
throw new NotImplementedException (); | |
} | |
} | |
object System.Collections.ICollection.SyncRoot { | |
get { | |
throw new NotImplementedException (); | |
} | |
} | |
bool System.Collections.ICollection.IsSynchronized { | |
get { | |
throw new NotImplementedException (); | |
} | |
} | |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () { | |
throw new NotImplementedException (); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment