我有多个IWindsorInstallers实现。在他们的Install方法中,我希望他们使用一些预定义的值。我希望使用IConfigurationStore来做到这一点,但我不清楚如何做到这一点。
我似乎找不到任何代码示例。
我的解决方法是首先将这些值作为对象注册到容器中,然后在需要值的安装程序中解析它们。但我不认为这是一个非常好的解决方案。
答案 0 :(得分:0)
您可以抽象出控制台和环境设置。您可以使用@PhilDegenhardt建议的文章。示例可能如下所示 - 控制台设置来自静态类到界面,环境设置来自方法。您可以组合/优化和移动类,以避免应用程序之间的错误依赖关系,以达到所需的行为:
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;
using ConsoleAppStrongTypeConfig;
namespace CastleDictionaryAdapterExample
{
class Program
{
static void Main()
{
GetUserSettings();
var programComponent = ContainerRegistrar.Container.Resolve<IProgramServiceConfigDependent>();
programComponent.ConfigDependentAction();
programComponent = ContainerRegistrar.Container.Resolve<IProgramServiceConfigDependent>();
programComponent.ConfigDependentAction();
Console.ReadLine();
}
private static void GetUserSettings()
{
ContainerRegistrar.SetupContainer();
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 IProgramServiceConfigDependent
{
void ConfigDependentAction();
}
class ProgramServiceConfigDependent : IProgramServiceConfigDependent
{
private readonly IConsoleSettings _consoleSettings;
private readonly IEnvironmentSettings _envSettings;
public ProgramServiceConfigDependent(IConsoleSettings consoleSettings, IEnvironmentSettings envSettings)
{
_consoleSettings = consoleSettings;
_envSettings = envSettings;
}
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);
}
}
public static 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()
);
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;
}
}
}
<强>更新强>
Here是Azure / MVC的示例项目,another one是控制台应用程序的示例项目。两者都用于强类型配置并抽象出ConfigurationManager。