Castle Windsor Fluent API:显式定义依赖项 [英] Castle Windsor Fluent API: Define dependency explicitly

查看:21
本文介绍了Castle Windsor Fluent API:显式定义依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下配置

        Container.Register(Component.For<A>().Named("foo"));
        Container.Register(Component.For<B>().Named("foobar"));

        Container.Register(
            AllTypes.Pick()
            .FromAssemblyNamed("MyAssembly")
            .If(t => t.Name.EndsWith("ABC"))
            .Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
            .WithService.Select(i => typeof(I))
        );

        Container.Register(
            AllTypes.Pick()
            .FromAssemblyNamed("MyAssembly")
            .If(t => t.Name.EndsWith("123"))
            .Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
            .WithService.Select(i => typeof(I))
        );

如果我知道接口I"公开了一个属性P",并且类A和B可以分配给P;我如何明确声明来自 AllTypes 调用的第一个类型集合应将属性 P 设置为 ID 为foo"的类型,而第二个集合应将相同的属性设置为 ID 为foobar"的类型?

If I know that the interface "I" exposes a property "P", and that the classes A and B can be assigned to P; how do I explicitly state that the first collection of types from the AllTypes call should have the property P set to the type with id of "foo", and the second collection should have the same property set to the type with the id of "foobar"?

使用 XML 配置,这可以通过使用 ${id} 符号显式设置参数来完成.我假设它在 fluent API 中类似.

Using XML config this can be done by explicitly setting the parameters using the ${id} notation. I assume its similar in the fluent API.

谢谢.

推荐答案

您走在正确的轨道上 - 您需要做的是配置每个组件的参数,为名为P"的参数提供值${foo}" 或 "${foobar}" 取决于您的方案,这是一个有效的 xunit 事实(向下滚动到实际注册代码的底部),它演示了您的方案.

You are on the right track - what you need to do is configure the parameters of each component, supplying the parameter named "P" with the value "${foo}" or "${foobar}" depending on your scenario, here's a working xunit fact (scroll down towards the bottom for the actual registration code) which demonstrates your scenario.

namespace Question651392
{
  public class First123 : I
  {
    public AbstractLetter P { get; set; }
  }

  public class Second123 : I
  {
    public AbstractLetter P { get; set; }
  }

  public class FirstABC : I
  {
    public AbstractLetter P { get; set; }
  }

  public class SecondABC : I
  {
    public AbstractLetter P { get; set; }
  }

  public interface I
  {
    AbstractLetter P { get; set; }
  }

  public abstract class AbstractLetter
  {
  }

  public class B : AbstractLetter
  {
  }

  public class A : AbstractLetter
  {
  }

  public class RegistrationFacts
  {
    [Fact]
    public void EnsureParametersCanBeSetWhenRegisteringComponentsInBulk()
    {
      WindsorContainer Container = new WindsorContainer();

      Container.Register(Component.For<A>().Named("foo"));
      Container.Register(Component.For<B>().Named("foobar"));

      Container.Register(
          AllTypes.Pick()
          .FromAssembly(GetType().Assembly)
          .If(t => t.Name.EndsWith("ABC"))
          .Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
          .Configure(c=>c.Parameters(Parameter.ForKey("P").Eq("${foo}")))
          .WithService.Select(new[] { typeof(I) })          
      );

      Container.Register(
          AllTypes.Pick()
          .FromAssembly(GetType().Assembly)
          .If(t => t.Name.EndsWith("123"))
          .Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
          .Configure(c => c.Parameters(Parameter.ForKey("P").Eq("${foobar}")))
          .WithService.Select(new[] { typeof(I)})
      );

      var all = Container.ResolveAll<I>();

      var firstABC = all.Single(i => i is FirstABC);
      Assert.IsType(typeof(A), firstABC.P);

      var first123 = all.Single(i => i is First123);
      Assert.IsType(typeof (B), first123.P);

      Assert.Equal(4, all.Count());
    }
  }
}

希望这会有所帮助!

这篇关于Castle Windsor Fluent API:显式定义依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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