Reputation: 4898
I can resolve generic interfaces to generic classes like the common generic repository pattern.
IRepository<objectA> resolves to Repository<objectA>
IRepository<objectB> resolves to Repository<objectB>
But then for objectC
I need a concrete Repository, namely ObjectCRepository
that extends Repository<objectC>
. If I register this won't there be two different registrations for for IRepository<objectC>
and everything fails?
Upvotes: 1
Views: 809
Reputation: 5801
There will be two registrations, but they don't conflict. Unity is smart enough to prefer fully defined closed generics over open ones. For example this works fine:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="ILog_Interface" type="Common.ILog`1, Common2"/>
<alias alias="Logger_Class" type="Common.Logger`1, Common2"/>
<alias alias="DemoServiceLog_Interface" type="Common.ILog`1[[Services.Demo.DemoService, Services.Demo]], Common2"/>
<alias alias="DemoServiceLog_Class" type="Common.ServiceLogger`1[[Services.Demo.DemoService, Services.Demo]], Common2"/>
<container name="DemoService">
<register type="ILog_Interface" mapTo="Logger_Class"/>
<register type="DemoServiceLog_Interface" mapTo="DemoServiceLog_Class"/>
</container>
</unity>
Upvotes: 1