问题
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