With Unity (2.0) I register two named interfaces of IFoo, mapped to two different implementations:
Container
.RegisterType<IFoo, Foo1>("first")
.RegisterType<IFoo, Foo2>("second");
Well, I have a service like this:
public class ServiceImpl : IService {
public ServiceImpl(IFoo foo, IDependency dep, IJustAnother stuff) { ... }
public MyValueObject[] FindVOs() { ... }
}
If want to register two named services each depending on one IFoo, i write this:
Container
.RegisterType<IService, ServiceImpl>(
"first",
new InjectionConstructor(
new ResolvedParameter<IFoo>("first"),
new ResolvedParameter<IDependency>(),
new ResolvedParameter<IJustAnother>()
)
)
.RegisterType<IService, ServiceImpl>(
"second",
new InjectionConstructor(
new ResolvedParameter<IFoo>("second"),
new ResolvedParameter<IDependency>(),
new ResolvedParameter<IJustAnother>()
)
)
It works, but I find it ugly and not clever, because for each new implementation of IFoo I need to add two RegisterType calls.
Is there a better way (maybe with an extension)?
Just another question: how Windsor container deal with this? with auto-registration?
* EDIT *
For example, if I need a CompositeService like this:
public class CompositeService : IService {
public ServiceImpl(IService[] services) { _services = services; }
public MyValueObject[] FindVOs() {
return _services.SelectMany(_ => _.FindVOs() );
}
}
I could register this way:
Container
.RegisterType<IService, CompositeService>();
and Unity will inject all named registration in the composite constructor. The problem is that this way it works only giving a name to all of the IService. And i can’t inject IFoo[] instead of IService because IFoo is used only by ServiceImpl which is a particular implementation of IService. This is the reason I asked if there is a better way to register them, avoiding redundancy, in Unity.
I solved writing a
UnityContainerExtension(with its ownBuilderStrategy), that allows me to do this:that way i don’t need to register a new named
IServicefor each namedIFoo, for me very useful because I can add namedIFooregistrations in the XML configuration file.