如何使用不同的依赖两次注册相同的类 [英] How to register same class twice with different dependencies

查看:176
本文介绍了如何使用不同的依赖两次注册相同的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想配置温莎城堡创建同一类型的两个组件(美孚 - >的IFoo),但具有不同的构造函数的输入。我后来想创造另一个组件(类型酒吧 - 见下面的代码)时,要消耗两个组件。

I would like to configure Castle Windsor to create two components of same type (Foo -> IFoo), but with different constructor inputs. I would also later like to consume both components when creating another component (type Bar - see code below).

public interface IFoo { }

public class Foo : IFoo
{
    private string _prop;

    public Foo(string prop)
    {
        _prop = prop;            
    }
}

public class Bar
{
    private IFoo _fooAbc;
    private IFoo _foo123;

    public Bar(IFoo fooAbc, IFoo foo123)
    {
        _foo123 = foo123;
        _fooAbc = fooAbc;
    }
}

在组件安装程序我试图注册组件这样的:

In component installer I tried registering components like this:

public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Classes.FromThisAssembly()
                            .BasedOn<IFoo>().WithServiceBase()
                            .ConfigureFor<Foo>(c => c.DependsOn(Dependency.OnValue<string>("abc")).Named("fooAbc"))
                            .ConfigureFor<Foo>(c => c.DependsOn(Dependency.OnValue<string>("123")).Named("foo123")));

        container.Register(Component.For<Bar>());   //?? specify which service to use
    }



但城堡抛出一个异常注册。所以,我怎么可以配置美孚的两个实例,其中一个ABC,另一个为123的依赖?此外,我后来想构建栏时,正确地分配他们,让fooAbc作为一个构造函数输入,foo123为第二位。我的最终目标是成功地解决吧。

But castle throws an registration exception. So how can I configure two instances of Foo, one with "abc" and another with "123" dependency? Also I would later like to correctly assign them when constructing Bar, so that fooAbc is used as first constructor input, and foo123 as second. My end goal would be to successfully resolve Bar.

推荐答案

我不知道这是否是更接近你问对,但是,你可以用
ServiceOverride.ForKey 来指定哪些参数映射到的名称:

I'm not sure if this is closer to what you're asking for, but, you can use ServiceOverride.ForKey to specify which parameters map to which names:

Component.For<Bar>().ImplementedBy<Bar>().
    DependsOn(ServiceOverride.ForKey("fooAbc").Eq("abc")).
    DependsOn(ServiceOverride.ForKey("foo123").Eq("123"))
);



另外,没有直接回答,但你有一个选项是解决一个的IEnumerable<的IFoo> 。这是一个很好的选择,如果你确实有的IFoo 任意数量来解决。

Alternatively, not a direct answer, but an option you have is to resolve an IEnumerable<IFoo>. This is a good option if you actually have an arbitrary number of IFoo to resolve.

如果你改变了定义律师接受了的IEnumerable

If you change the definition of Bar to accept an IEnumerable

public class Bar
{
    private readonly IEnumerable<IFoo> _foos;

    public Bar(IEnumerable<IFoo> foos)
    {
        _foos = foos;
    }
}



然后为注册和解析。你需要你做登记之前添加的决心

Then to register and resolve. You need to add the Resolve before you do the registrations.

var container = new WindsorContainer();

container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel, true));

container.Register(
    Component.For<IFoo>().Instance(new Foo("abc")).Named("abc"),
    Component.For<IFoo>().Instance(new Foo("123")).Named("123"),
    Component.For<Bar>().ImplementedBy<Bar>());

这篇关于如何使用不同的依赖两次注册相同的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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