【问题标题】:Argument passed is not favored over default implementation in Castle Windsor在 Castle Windsor 中,参数传递不优于默认实现
【发布时间】:2013-01-27 18:39:44
【问题描述】:

我希望参数实现优于默认注册的实现,但它不起作用。

不正确的偏好演示

    private static void Main(string[] args)
    {
        PassParamThatsAlreadyRegisteredAtResolutionTime();
        Console.ReadLine();
    }

    private static void PassParamThatsAlreadyRegisteredAtResolutionTime()
    {
        Console.WriteLine("Passing argument that is already registered 
                    does not take precedence over the default implementation");
        var container = new WindsorContainer();
        container.Register(
            Component.For<ISimpletonManager>()
                     .ImplementedBy<SimpletonManager>()
                     .LifestyleTransient()
                     .Properties(PropertyFilter.IgnoreAll));
        container.Register(Component.For<ISimpleton>().UsingFactoryMethod(() =>
                                     new Simpleton("Default Implementation"))
                                    .LifestyleTransient());
        // The above line could equally be the following, result is the same:
        // container.Register(Component.For<ISimpleton>()
        //                     .ImplementedBy<Simpleton>().LifestyleTransient());
        var runtimeConstructorParam = new Simpleton("Passed In Implementation");
        var runtimeArguments = new Arguments(
                                 new object[] {runtimeConstructorParam});
        var shouldBeManagerWithPassedInSimpleton = container
                             .Resolve<ISimpletonManager>(runtimeArguments);
        Console.WriteLine(shouldBeManagerWithPassedInSimpleton.Log);
    }

控制台输出

Passing argument that is already registered
does not take precedence over the default implementation
Birth With Child Simpleton: Default Implementation

如何反转偏好?

  • 我需要能够忽略默认注册的依赖项,而是使用提供的参数作为 ISimpleton 依赖项进行 Castle Windsor 解析吗?
  • 我是否需要实现自己的IDependencyResolver?怎么样?
  • 或者DynamicParameters在这里有用吗?

提供的依赖 - Simpleton 类

public class Simpleton : ISimpleton
{
    public Simpleton(string id)
    {
        Id = id;
    }

    public string Id { get; set; }
}

已解析类型 - SimpletonManager

public class SimpletonManager : ISimpletonManager
{
    private ISimpleton _child;

    public SimpletonManager(ISimpleton simpleton)
    {
        Child = simpleton;
    }

    public ISimpleton Child
    {
        get { return _child; }
        set
        {
            _child = value;
            Log = "Birth With Child Simpleton: " + Child.Id;
        }
    }

    public string Log { get; private set; }
}

[Using Castle.Core.dll and Castle.Windsor.dll 3.1.0 (2012-08-05)]

【问题讨论】:

标签: c# .net dependency-injection castle-windsor constructor-injection


【解决方案1】:

your other question 一样,依赖的类型与Arguemnts 暴露的类型不同

【讨论】:

  • 谢谢!抱歉,我之前把示例代码弄得一团糟,我累了 :) 我更新了代码来说明问题,这就是我如何让 Castle Windsor 更喜欢在解决时提供的依赖项而不是注册时提供的默认实现?基本上,当在解析时没有提供任何参数(默认)时,我需要注册时实现,实际上我还需要完全不同的类的注册时实现,但是每当我在解析时提供实现时,我都需要温莎使用那个,这不会发生?
  • 维基!该决议附带var runtimeArguments = new Arguments{{ typeof(ISimpleton), runtimeConstructorParam }};
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多