The title of this post might seem a little bit awkward, so I will explain briefly the topic of this
post then go directly to some sample codes.
When using the Microsoft Unity Block (URL) and register two type mappings for the same Interface each specificed by a name, and then
you want to resolve an Object that is dependent on an instance of the above Interface, you need to resolve one of the above two implementations
of the Interface, then resolve your Object that is dependent on an instance of the interface (both Constructor and Property Dependency Injection).
Here is a sample code to illustrate the above:
// Initialize a new Container
IUnityContainer container = new UnityContainer();
// Register the ITesting interface to map to Testing1 class that
// implements the interface and give this mapping a name "Testing1"
container.RegisterType<ITesting, Testing1>("Testing1");
// Register the ITesting interface to map to Testing1 class that
// implements the interface and give this mapping a name "Testing2"
container.RegisterType<ITesting, Testing2>("Testing2");
// Get an instance of the ITesting mapping object, in this case Testing1 class
ITesting iTestingObj = container.Resolve<ITesting>("Testing1");
// Resolve Testing class that has a constructor
// dependency injection on ITesting interface
Testing testingObj = container.Resolve<Testing>();
Without first of all resolving an instance of the ITesting interface based on
one of the above registered type mappings, resolving Testing class and its dependent interface
would not have worked.
Hope this post helps!
Regards
Tags: Microsoft Unity Block, Unity Block