我有一个这样的构造函数的类
public class BlotterService : IBlotterService
{
public BlotterService(IAService aService, IAnotherService anotherService)
{
// ...
}
}
像这样注册依赖
// Default lifecycle is Singleton
Component.For<IAnotherService >().ImplementedBy<AnotherService >()
Component.For<IAService>().ImplementedBy<AService>() // instance 1 of AService
Component.For<IAService>().ImplementedBy<AService>().Named(
"aServiceAlternativeForBlotter") // instance 2 of AService
Component.For<IBlotterService>().ImplementedBy<BlotterService>()
通常,当实例化需要 IAService 的类时,我需要 AService 的第一个实例,但是在 BlotterService 的特定情况下,我需要 AService 的实例 2。
如何向城堡容器注册 BlotterService/AService 以实现此目的?
编辑:更新
我正在使用 CaSTLe 2.5.1.0,遗憾的是无法升级。
最佳答案
将上次注册更改为:
Component
.For<IBlotterService>()
.ImplementedBy<BlotterService>()
.DependsOn(Dependency.OnComponent(
typeof(IAService),
"aServiceAlternativeForBlotter"))
关于c# - 使用 CaSTLeWindsor 2.5.1.0 指定要在 ctor 注入(inject)中使用的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9018488/