Map interface with 1 generic to instance with 2 generics using app.config or custom type resolver?

Jul 15, 2008 at 6:34 PM
Let's say that I have the following classes/interfaces:
- IBusinessFacade<T>
- BusinessFacade<TInterface, TConcrete>
- IPerson
- Person

When I create an instance of BusinessFacade<> from code, I need to do something like this:
IBusinessFacade<IPerson> facade = new BusinessFacade<IPerson, Person>();

(The reason it's built this way is that BusinessFacade<> wraps all my NHibernate logic.  While I'm trying to be interface-only, NHibernate unfortunately needs to know the concrete types for some operations.)

If I use app.config to map IPerson to Person and IBusinessFacade<T> to BusinessFacade<TInterface, TConcrete>, I get an exception:
"The supplied type IBusinessFacade`1 does not have the same number of generic arguments as the target type BusinessFacade`2."

This error makes sense because Unity has no idea where that second generic parameter (TConcrete) is supposed to come from.

1. Is there any way to specify generics in the Unity constructor config?
2. Is it possible to create a custom type resolver IBusinessFacade<T> in order to get around this? 

Thanks,
-Ben Day
Microsoft MVP for C#
http://blog.benday.com

Jul 16, 2008 at 1:01 AM
It's not supported in the current version, but we're doing a ton of work to make generics work better for the next release. In the meantime, you can write some custom resolution stuff; it's supported but not documented. I'm currently working on extensibility docs which will include that. Custom resolution is one of the topics for our "Extending Unity" webcast on July 24th.

Oct 1, 2009 at 7:20 AM

Hello, I want to know if this issue is resolved in version 1.2

And How Can I do that?

Thank in advance.

Guille.-

 

Dec 18, 2009 at 1:34 AM

This scenario didn't make the cut for 1.2, and it's not looking good for V2.0 either, at least not directly.

You'd need to write some container extension stuff to do it, but the docs never got written either.

Jan 23, 2013 at 7:32 AM

Is this scenario supported in 2.1 release?

I am facing same issue like mapping between
- IBusinessFacade<T>
- BusinessFacade<TInterface, TConcrete>

And I am getting same exception.

Regards

Amit

Jan 24, 2013 at 5:31 AM
Edited Jan 24, 2013 at 5:31 AM

No, unfortunately, it is still not supported in Unity 2.1.

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

Jan 25, 2013 at 11:26 AM

It's actually quite bizarre to have interfaces for entities. Interfaces are there to abstract behavior, but entities don't have behavior, or at least, won't have any behavior that would typically need to abstracted. Question yourself, do I have multiple classes that implement IPerson? Do I ever want to decorate IPerson? If the answer is no, IPerson is probably a useless abstraction and it should be removed. Entities should typically not be resolved by a container, because they are not 'services' and their lifetime is controlled by a unit of work, not by the container.