Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dependency injection 如何使用Castle Windsor配置装饰器?_Dependency Injection_Castle Windsor_Decorator - Fatal编程技术网

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)
{
集装箱。登记(
用于()的组件
.由()实施
);
}
}
然而,我仍然需要告诉它两件事

  • 当它解析为IamUsedWice时,我希望它从现在开始解析DecoratingIsFun的实例,而不是其他类
  • 当解析DecoratingIsFun时,我需要将它解析为它所创建实例的构造函数参数
  • 目标是我可以调用windsorContainer.Resolve()并获得一个DecoratingIsFun实例


    我怎样才能告诉安装程序这样做呢?

    为了让
    装饰变得有趣
    能够真正地装饰
    ,您需要确保
    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中。