温莎城堡地图命名组件要具体属性 [英] Castle Windsor Map Named Component To Specific Property
问题描述
以下情形:
我们用流利的API来注册所有两种成分tyepof(A)与命名的键组件的装配和。另外B类具有两个属性的typeof(A)应该得到指定的组件注入。
We use the Fluent API to register all components in an assembly and two components tyepof(A) with named keys. Another class B with two properties typeof(A) should get the named components injected.
示例:
public class A : IA {}
public class B : IB
{
[Named("first")]
public IA First { get; set; }
[Named("second")]
public IA Second { get; set; }
}
// ...
container.Register(Component.For<IA>().Instance(new A(value1)).Named("first"));
container.Register(Component.For<IA>().Instance(new A(value2)).Named("second"));
// ...
var b = container.Resolve<IB>(); // named instances of A get injected to B using the Named attribute
这可能与属性类似名称或仅与XML配置?
Is this possible with an attribute like Named or only with the Xml Config?
推荐答案
标准的方式来做到这一点的温莎使用的服务覆盖的。在您的例子,当你注册 B
你会做这样的:
The standard way to do this in Windsor is using service overrides. In your example, when you register B
you'd do it like this:
container.Register(Component.For<IB>().ImplementedBy<B>()
.ServiceOverrides(new {First = "first", Second = "second"}));
(还有其他的方法来EX preSS这一点,请检查链接的文档)
(there are other ways to express this, check the linked docs)
使用的
属性,你提出污染code与无关的关注( B
不应该关心什么 A
取值得到注射)
Using a Named
attribute as you propose pollutes the code with unrelated concerns (B
should not care about what A
s get injected)
这篇关于温莎城堡地图命名组件要具体属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!