Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register multiple types that implement the same interface

I have a single interface and this is being used by 2 classes. I am using unity configuration to identify the instance based on the interface.

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

like image 335
Saravanan Avatar asked Jul 26 '26 21:07

Saravanan


1 Answers

This 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")));
like image 68
youwhut Avatar answered Jul 30 '26 02:07

youwhut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!