Windsor Ioc container: How to register that certain constructors take different implementation of an interface

风格不统一 提交于 2019-12-08 10:03:09

问题


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

e.g.

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

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

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

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.)

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


回答1:


Use Service Overrides:

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"))
);


来源:https://stackoverflow.com/questions/7849692/windsor-ioc-container-how-to-register-that-certain-constructors-take-different

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!