One interface multiple implementaion issue

Jul 24, 2013 at 10:57 AM
Edited Jul 24, 2013 at 11:11 AM
Just started with Unity today.

Page 37 in Developer's guide to Microsoft unity the below code is given

var container = new UnityContainer();
container.RegisterType<ITenantStore, TenantStore>();

But there is no explanation at all for the situation if there are multiple implementation of the ITenetStore interface. At this point this question is easily arise in mind. how Unity handles this should be explained here even in a pointer manner that detail description is given in another chapter of section.

var container = new UnityContainer();
container.RegisterType<ITenantStore, AnotherTenantStore>();

Any one here to help me..? Can Unity handle this kind of situation? If yes how..?

And please don't say, using factory/abstract factory/service locator, unity handles such situation.
Jul 24, 2013 at 11:11 AM
If you have multiple implementations of the same interface you must used named registrations.
var container = new UnityContainer();
container.RegisterType<ITenantStore, TenantStore>(); // default registration
container.RegisterType<ITenantStore, AnotherTenantStore>("another");
Jul 24, 2013 at 12:07 PM
Thank you for the quick reply. It is helpful.

But, it means that, as registration is mainly done in "Application start methods", if new implementation comes I need to recompile my application?

Any work around?
Jul 24, 2013 at 12:15 PM
Edited Jul 24, 2013 at 12:16 PM
Unity 3 comes with support for registration conventions. They would automatically adapt your registration code to additional implementations.

Have a look a Grigori Melnik's post about that topic.
Editor
Jul 24, 2013 at 1:52 PM
@abhisheks, Page 48 of the Guide discusses named registrations with an example of registering the same interface with 2 different constructors for the same interface. So, the concept is there, but you are right, I didn't see anything explicitly going over a more simple (and direct) case of one interface to multiple concrete implementations. Thanks for the feedback.

@weberse is right, if you want to register arbitrary implementations of a certain interface (or other similar scenarios) without recompilation then registration by convention is the way to go.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to