温莎城堡的解决方法:为什么要传递参数?他们是干什么的? [英] Castle Windsor Resolve method: why pass arguments? What are they for?

查看:64
本文介绍了温莎城堡的解决方法:为什么要传递参数?他们是干什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对温莎城堡的解决方法感到困惑。这种方法使我几乎可以通过任何内容。是在resolve方法中提交的值传递并在最终解析为该对象的构造函数中使用,还是用于帮助解析程序确定要使用的具体实现?

I am confused by Castle Windsor resolve method. This method allows me to pass almost anything. Is the value submitted in the resolve method passed along and used in the constructor of the object which is eventually resolved to, or is this value used to help the resolver determine what concrete implementation to use?

例如,如果我有以下代码段...

For example, if I have the following snippet...

var _container = new WindsorContainer();
_container.Install(FromAssembly.This());

var MyProcessor = _container.Resolve<IProcessor>(new Arguments(new {"Processor1"}));

假设我有两个IProcessor的具体实现-如Processor1:IProcessor和/或Processor2:IProcessor。

assuming I have two concrete implementations of IProcessor - like Processor1:IProcessor, and/or Processor2:IProcessor. What are the 'Arguments' used for?

我理解...

Component.For<IProcessor>() 

...我为温莎人选择使用的术语(即DependsOn或ServicesOverrides)和意图而苦苦挣扎。给定该方法称为解决的方法,我只能将传递给此方法的任何值成像,以解决使用哪种具体实现的决策。这个假设是错误的吗?

... needs to be defined, but I am struggling with the terms the Windsor folks choose to use (i.e. DependsOn, or ServicesOverrides) and the intent. Given the method is called 'resolve' I can only image any values passed to this will be used to resolve the decision on which concrete implementation to use. Is this assumption wrong?

推荐答案

您正在谈论的arguments参数用于为无法满足要求的组件提供参数由温莎组件。匿名类型超载以及我相信的字典整体都是用于此目的的。我过去曾使用过此方法,因此不建议使用它,因为它会导致出现Cristiano提到的不良模式……而上次使用时,它仅适用于直接解析的组件。无论如何,这是一个工作原理示例:

The arguments parameter you're talking about is for providing arguments to components that can't be satisfied by Windsor components. The anonymous types overloads as well as the dictionary overalls I believe are all for this purpose. I've used this in the past, and I don't recommend it as it leads to poor patterns like Cristiano mentioned... and last time I used this it only works for the component being directly resolved. Anyway... here's an example of how this works:

[TestFixture]
public class Fixture
{
    [Test]
    public void Test()
    {
        IWindsorContainer container = new WindsorContainer();
        container.Register(Component.For<IFoo>().ImplementedBy<Foo>().LifeStyle.Is(LifestyleType.Transient));

        Assert.Throws<HandlerException>(() => container.Resolve<IFoo>());

        IFoo foo = container.Resolve<IFoo>(new {arg1 = "hello", arg2 = "world"});
        Assert.That(foo, Is.InstanceOf<Foo>());
        Assert.That(foo.ToString(), Is.EqualTo("hello world"));
    }
}

public interface IFoo
{

}

public class Foo : IFoo
{
    private readonly string _arg1;
    private readonly string _arg2;

    public Foo(string arg1, string arg2)
    {
        _arg1 = arg1;
        _arg2 = arg2;
    }

    public override string ToString()
    {
        return string.Format("{0} {1}", _arg1, _arg2);
    }
}

这篇关于温莎城堡的解决方法:为什么要传递参数?他们是干什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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