Forked from BenHall/CastleDictionaryAdapterExample.cs
Last active
August 29, 2015 14:23
-
-
Save ognyandim/cf1675e13b60b9a5145e to your computer and use it in GitHub Desktop.
Abstracting away application configuration - runtime user provided, environment provided, app settings provided
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
using System; | |
using System.Collections; | |
using System.Configuration; | |
using Castle.Components.DictionaryAdapter; | |
using Castle.Facilities.TypedFactory; | |
using Castle.MicroKernel; | |
using Castle.MicroKernel.Context; | |
using Castle.MicroKernel.Registration; | |
using Castle.Windsor; | |
namespace ConsoleAppStrongTypeConfig | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
ContainerRegistrar.SetupContainer(); | |
GetUserSettings(); | |
var programComponent = ContainerRegistrar.Container.Resolve<IProgramServiceConfigDependent>(); | |
programComponent.ConfigDependentAction(); | |
programComponent = ContainerRegistrar.Container.Resolve<IProgramServiceConfigDependent>(); | |
programComponent.ConfigDependentAction(); | |
Console.ReadLine(); | |
} | |
private static void GetUserSettings() | |
{ | |
Console.WriteLine("Enter setting 1"); | |
Console.WriteLine(); | |
string setting1 = Console.ReadLine(); | |
Console.WriteLine("Enter setting 2"); | |
Console.WriteLine(); | |
string setting2 = Console.ReadLine(); | |
GlobalUserConsoleSettings.ConsoleSettings = new DictionaryAdapterFactory().GetAdapter<IConsoleSettings>( | |
new Hashtable() { | |
{ "ConsoleSetting1", setting1 }, | |
{ "ConsoleSetting2", setting2 } } | |
); | |
} | |
} | |
public static class GlobalUserConsoleSettings | |
{ | |
public static IConsoleSettings ConsoleSettings { get; set; } | |
} | |
public interface IConsoleSettings | |
{ | |
string ConsoleSetting1 { get; set; } | |
int ConsoleSetting2 { get; set; } | |
} | |
public interface IEnvironmentSettings | |
{ | |
string EnvironmentSetting1 { get; set; } | |
int EnvironmentSetting2 { get; set; } | |
long EnvironmentSettingWhichChanges { get; set; } | |
} | |
public interface IApplicationConfiguration | |
{ | |
bool EnableNewsletterSignup { get; set; } | |
} | |
public interface IProgramServiceConfigDependent | |
{ | |
void ConfigDependentAction(); | |
} | |
class ProgramServiceConfigDependent : IProgramServiceConfigDependent | |
{ | |
private readonly IConsoleSettings _consoleSettings; | |
private readonly IEnvironmentSettings _envSettings; | |
private readonly IApplicationConfiguration _applicationConfiguration; | |
public ProgramServiceConfigDependent( | |
IConsoleSettings consoleSettings, | |
IEnvironmentSettings envSettings, | |
IApplicationConfiguration applicationConfiguration) | |
{ | |
_consoleSettings = consoleSettings; | |
_envSettings = envSettings; | |
_applicationConfiguration = applicationConfiguration; | |
} | |
public void ConfigDependentAction() | |
{ | |
Console.WriteLine("Doing stuff."); | |
Console.WriteLine("Console setting 1 :" + _consoleSettings.ConsoleSetting1); | |
Console.WriteLine("Console setting 2 :" + _consoleSettings.ConsoleSetting2); | |
Console.WriteLine("Environment setting 2 :" + _envSettings.EnvironmentSetting1); | |
Console.WriteLine("Environment setting 2 :" + _envSettings.EnvironmentSetting2); | |
Console.WriteLine("Environment setting which changes with time :" + _envSettings.EnvironmentSettingWhichChanges); | |
Console.WriteLine("Application setting 1 :" + _applicationConfiguration.EnableNewsletterSignup); | |
} | |
} | |
public class ContainerRegistrar | |
{ | |
internal static WindsorContainer Container { get; set; } | |
public static void SetupContainer() | |
{ | |
var container = new WindsorContainer(); | |
container.AddFacility<TypedFactoryFacility>(); | |
container.Register( | |
Component.For<IConsoleSettings>().UsingFactoryMethod(GetGlobalUserConsoleSettings), | |
Component.For<IEnvironmentSettings>().UsingFactoryMethod(GetLiveEnvironmentSettings).LifestyleTransient(), | |
Component.For<IProgramServiceConfigDependent>().ImplementedBy<ProgramServiceConfigDependent>().LifestyleTransient(), | |
Component.For<IApplicationConfiguration>().UsingFactoryMethod(() => new DictionaryAdapterFactory().GetAdapter<IApplicationConfiguration>(ConfigurationManager.AppSettings)).LifestyleTransient() | |
); | |
Container = container; | |
} | |
private static IConsoleSettings GetGlobalUserConsoleSettings(IKernel k, CreationContext c) | |
{ | |
return GlobalUserConsoleSettings.ConsoleSettings; | |
} | |
private static IEnvironmentSettings GetLiveEnvironmentSettings(IKernel k, CreationContext c) | |
{ | |
string envSet1 = Environment.CurrentDirectory; | |
int envSet2 = Environment.OSVersion.Version.Major; | |
long envSet3 = DateTime.UtcNow.Ticks; | |
IEnvironmentSettings envSet = new DictionaryAdapterFactory().GetAdapter<IEnvironmentSettings>( | |
new Hashtable() { | |
{ "EnvironmentSetting1", envSet1 }, | |
{ "EnvironmentSetting2", envSet2 }, | |
{ "EnvironmentSettingWhichChanges", envSet3}} | |
); | |
return envSet; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment