如何使用温莎城堡配置装饰器? [英] How do I configure a decorator with Castle Windsor?

查看:72
本文介绍了如何使用温莎城堡配置装饰器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个装饰器和实际的实现,如下所示:

I have a decorator and actual implementation that looks like this:

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();
    }
}

并且在我开始使用此配置之前已经进行了配置实际实现的xml,如下所示:

and the configuration was set up before I started this using xml for the actual implementation and looks something like this:

<component id="forReal"
           service="SomeNamespace.IAmUsedTwice, SomeNamespace"
           type="SomeNamespace.ForReal, SomeNamespace">
  <parameters>
    <iNeed>${iNeed}</iNeed>        
  </parameters>
</component>

,您可以假定iNeed组件已正确设置。

and you can assume that the iNeed component is set up correctly already.

现在,系统已经配置为使用ForReal类,但是我想做的是换出ForReal类并立即使用DecoratingIsFun类。

Now, the system is already configured to use the ForReal class, but what I want to do is swap out the ForReal class and use the DecoratingIsFun class now.

我创建了一个安装程序来注册DecoratingIsFun类,如下所示:

I created an installer to register the DecoratingIsFun class like so:

public class DecoratorInstaller: IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(
            Component.For<IAmUsedTwice>()
                .ImplementedBy<DecoratingIsFun>()
        );
    }
}

但是,我仍然需要告诉两件事。

However, I still need to tell it two things.


  1. 当它解析IAmUsedTwice时,我希望它从现在开始解析DecoratingIsFun的实例,而不是其他类

  2. 在解决DecoratingIsFun时,我需要它来将ForReal解析为其正在创建的实例的构造函数参数。

然后我可以调用windsorContainer.Resolve()并获取DecoratingIsFun实例。

The goal will be that I can then call windsorContainer.Resolve() and get a DecoratingIsFun instance.

如何告诉安装程序执行该操作?

How can I tell the installer to do that?

推荐答案

为了允许 DecoratingIsFun 装饰 ForReal ,则需要确保在之前 ForReal 注册 DecoratingIsFun -温莎将正确解析装饰器,并通过实现 IAmUsedTwice 的下一次注册来满足其依赖性。

In order to allow DecoratingIsFun to decorate ForReal, you need to ensure that DecoratingIsFun is registered before ForReal - then Windsor will correctly resolve the decorator and satisfy its dependency with the next registration of something that implements IAmUsedTwice.

但是由于您使用的是XML要注册第一个服务,我不知道该怎么实现,因为在实例化 WindsorContainer 时XML被吸收了。

But since you're using XML to register the first service, I don't know how to achieve that, since the XML is sucked up when you instantiate WindsorContainer.

但是为什么首先要使用XML?是因为您不认为可以使用XML来配置组件,除非您还使用它来注册组件?

But why are you using XML in the first place? Is it because you don't think that you can use XML to configure components unless you're also using it to register them?

如果是这种情况,则应将XML简化为以下形式:

If that is the case, you should reduce the XML to something like this:

<component id="forReal">
    <parameters>
        <iNeed>${iNeed}</iNeed>        
    </parameters>
</component>

并将注册移到您的代码中,从而可以控制注册顺序。然后,确保在 .Named( forReal)中注册了 ForReal 。该实例已解决。

and move the registration to your code, allowing you to control the order of registration. Then, make sure that ForReal is registered with .Named("forReal"), allowing the configuration to be matched when the instance is resolved.

这篇关于如何使用温莎城堡配置装饰器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆