c# - 使用 CaSTLeWindsor 2.5.1.0 指定要在 ctor 注入(inject)中使用的依赖项

标签 c# dependency-injection castle-windsor

我有一个这样的构造函数的类

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/

相关文章:

c# - 将复选框添加到 UniformGrid

c# - 使用按钮使用户控件在使用 MVVM 模式的 WPF 中可见

java - Spring - 从工厂类注入(inject) bean

dependency-injection - 当流畅的调度程序作业调用方法时,无法访问已处置的对象异常

c# - CaSTLe.Windsor 生活方式取决于上下文?

c# - 是否可以调试Visual Studio查看SqlConnection事务

c# - 将 MDI 子菜单项合并到父菜单中

angular - 有没有办法根据 Angular 2中的路线注入(inject)不同的服务?

wcf - CaSTLe Windsor 组件在单个服务上注册多个接口(interface)

c# - 在 MVC 中设置 CaSTLe Windsor