Windsor Ioc容器:如何注册某些构造函数采用接口的不同实现 [英] Windsor Ioc container: How to register that certain constructors take different implementation of an interface

查看:50
本文介绍了Windsor Ioc容器:如何注册某些构造函数采用接口的不同实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多使用IMyService作为构造函数参数的类。

I have lots of classes that take an IMyService as a constructor argument.

例如

ClassA(IMyservice myservice)
// this should take a Concrete1 for IMyService

ClassB(IMyservice myservice)
// this should take a Concrete2 for IMyService

我该如何注册,以便ClassB获得Concrete2,ClassA获得Concrete1?

How do I do my registration so that ClassB gets a Concrete2 and ClassA gets a Concrete1?

还有,有没有一种方法可以使默认值成为默认值,而仅指定与默认值不同的实例? (因为大多数将使用Concrete1,而少数将使用Concrete2。)

Plus, is there a way to make one the default and only specify the instances that deviate from the default? (As the majority will take a Concrete1 and only a small number will take a Concrete2.)

我正在使用流畅的界面, XML配置。

I am using the fluent interface, NOT the XML configuration.

推荐答案

使用服务覆盖:

var container = new WindsorContainer();
container.Register(
    Component.For<IMyService>().ImplementedBy<Concrete1>().Named("C1"),
    Component.For<IMyService>().ImplementedBy<Concrete2>().Named("C2"),
    Component.For<ClassA>().ServiceOverrides(ServiceOverride.ForKey("service").Eq("C1")),
    Component.For<ClassB>().ServiceOverrides(ServiceOverride.ForKey("service").Eq("C2"))
);

这篇关于Windsor Ioc容器:如何注册某些构造函数采用接口的不同实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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