How to register several types that implement the same interface

I have one interface and this is used by 2 classes. I use a unity configuration to identify an instance based on an interface.

Now I want to know how I should register these types so that I can call the appropriate implementation based on the very separate interface.

+3
source share
2 answers

Here is how I do it:

        var container = new UnityContainer().RegisterType<IAmImplementedMoreThanOnce, Implementation1>("Implementation1")
                                            .RegisterType<IAmImplementedMoreThanOnce, Implementation2>("Implementation2")
                                            .RegisterType<IHaveDependencies1, WithDependenciesImplementation1>(new InjectionConstructor(new ResolvedParameter<IAmImplementedMoreThanOnce>("Implementation1")))
                                            .RegisterType<IHaveDependencies2, WithDependenciesImplementation2>(new InjectionConstructor(new ResolvedParameter<IAmImplementedMoreThanOnce>("Implementation2")));
+6
source

. , . , . , .

, , .

.

ClassA: IClass B: ?

, ; ClassA: IClassA, ClassB: IClassB IClassA: IClass, IClassB: IClass IClassA ClassA IClassB ClassB.

RegisterType IClass, ClassA > ( "TypeA" );
RegisterType IClass, ClassB > ( "TypeB" );

IClass > ( "TypeA" );
Resolve IClass > ( "TypeB" );

?

-4

All Articles