如何使用不同的依赖两次注册相同的类

编程入门 行业动态 更新时间:2024-10-09 03:18:52
本文介绍了如何使用不同的依赖两次注册相同的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想配置温莎城堡创建同一类型的两个组件(美孚 - >的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>());

更多推荐

如何使用不同的依赖两次注册相同的类

本文发布于:2023-11-13 11:20:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   如何使用

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!