Dependency injection 如何使用Castle Windsor配置装饰器?
我有一个装饰器和实际实现,如下所示:Dependency injection 如何使用Castle Windsor配置装饰器?,dependency-injection,castle-windsor,decorator,Dependency Injection,Castle Windsor,Decorator,我有一个装饰器和实际实现,如下所示: public interface IAmUsedTwice { void DoSomething(); } public class ForReal: IAmUsedTwice { public SomethingElse Need { get; set; } public ForReal(SomethingElse iNeed) { Need = iNeed; } public void
public interface IAmUsedTwice
{
void DoSomething();
}
public class ForReal: IAmUsedTwice
{
public SomethingElse Need { get; set; }
public ForReal(SomethingElse iNeed)
{
Need = iNeed;
}
public void DoSomething()
{
Console.WriteLine("Realing doing something here");
}
}
public class SomethingElse {}
public class DecoratingIsFun: IAmUsedTwice
{
private IAmUsedTwice Actual { get; set; }
public DecoratingIsFun(IAmUsedTwice actual)
{
Actual = actual;
}
public void DoSomething()
{
Console.WriteLine("This is a decorator!");
Actual.DoSomething();
}
}
<component id="forReal"
service="SomeNamespace.IAmUsedTwice, SomeNamespace"
type="SomeNamespace.ForReal, SomeNamespace">
<parameters>
<iNeed>${iNeed}</iNeed>
</parameters>
</component>
<component id="forReal">
<parameters>
<iNeed>${iNeed}</iNeed>
</parameters>
</component>
配置是在我开始使用xml进行实际实现之前设置的,如下所示:
public interface IAmUsedTwice
{
void DoSomething();
}
public class ForReal: IAmUsedTwice
{
public SomethingElse Need { get; set; }
public ForReal(SomethingElse iNeed)
{
Need = iNeed;
}
public void DoSomething()
{
Console.WriteLine("Realing doing something here");
}
}
public class SomethingElse {}
public class DecoratingIsFun: IAmUsedTwice
{
private IAmUsedTwice Actual { get; set; }
public DecoratingIsFun(IAmUsedTwice actual)
{
Actual = actual;
}
public void DoSomething()
{
Console.WriteLine("This is a decorator!");
Actual.DoSomething();
}
}
<component id="forReal"
service="SomeNamespace.IAmUsedTwice, SomeNamespace"
type="SomeNamespace.ForReal, SomeNamespace">
<parameters>
<iNeed>${iNeed}</iNeed>
</parameters>
</component>
<component id="forReal">
<parameters>
<iNeed>${iNeed}</iNeed>
</parameters>
</component>
${iNeed}
您可以假设iNeed组件已经正确设置
现在,系统已经配置为使用ForReal类,但是我现在要做的是替换ForReal类并使用DecoratingIsFun类
我创建了一个安装程序来注册DecoratingIsFun类,如下所示:
public class DecoratorInstaller: IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Component.For<IAmUsedTwice>()
.ImplementedBy<DecoratingIsFun>()
);
}
}
公共类装饰安装程序:IWindsorInstaller
{
public void安装(IWindsorContainer、IConfigurationStore)
{
集装箱。登记(
用于()的组件
.由()实施
);
}
}
然而,我仍然需要告诉它两件事
我怎样才能告诉安装程序这样做呢?为了让
装饰变得有趣
能够真正地装饰,您需要确保DecoratingIsFun
在ForReal
之前注册-然后Windsor将正确解析decorator,并在下次注册实现IAmUsedTwice
的东西时满足其依赖性
但是,由于您使用XML注册第一个服务,我不知道如何实现这一点,因为当您实例化WindsorContainer
时,XML会被占用
但是为什么首先要使用XML呢?是不是因为您认为您不能使用XML来配置组件,除非您也使用它来注册组件
如果是这种情况,您应该将XML简化为如下内容:
public interface IAmUsedTwice
{
void DoSomething();
}
public class ForReal: IAmUsedTwice
{
public SomethingElse Need { get; set; }
public ForReal(SomethingElse iNeed)
{
Need = iNeed;
}
public void DoSomething()
{
Console.WriteLine("Realing doing something here");
}
}
public class SomethingElse {}
public class DecoratingIsFun: IAmUsedTwice
{
private IAmUsedTwice Actual { get; set; }
public DecoratingIsFun(IAmUsedTwice actual)
{
Actual = actual;
}
public void DoSomething()
{
Console.WriteLine("This is a decorator!");
Actual.DoSomething();
}
}
<component id="forReal"
service="SomeNamespace.IAmUsedTwice, SomeNamespace"
type="SomeNamespace.ForReal, SomeNamespace">
<parameters>
<iNeed>${iNeed}</iNeed>
</parameters>
</component>
<component id="forReal">
<parameters>
<iNeed>${iNeed}</iNeed>
</parameters>
</component>
${iNeed}
并将注册移动到您的代码中,以便您控制注册顺序。然后,确保ForReal
已注册到.Named(“ForReal”)
,以便在解析实例时匹配配置。谢谢您提供的信息。我之所以使用XML,是因为XML实际上来自遗留代码,所以它一直是我的“起点”。然而,我想开始将东西迁移到fluentapi,因此它的一部分在XML中,另一部分在fluentapi中。