You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 2, 2018. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 2, 2018. It is now read-only.
I am looking to use a Factory to create the generic object. I want the generic object type passed on to the implementation factory so that I can create the object based on the generic type.
services.AddTransient(typeof(ServiceDocument<>),// I need to get the closed generic type here so that the corresponding object is created, i.e., ServiceDocument<Employee>
p =>newServiceDocument<Employee>());
As you see in the above code, in the implementationFactory function, I want to create the object based on the generic type, for example:
- For ServiceDocument<Employee>, I need to create the ServiceDocument<Employee>
- For ServiceDocument<Department>, I have to create the ServiceDocument<Department> object
Is there a way the DI can pass in the object type to this factory function?
I really don't see how this can be supported without doing MakeGenericType somewhere in the factory implementation. That would not be all that efficient
Activity
davidfowl commentedon Jan 17, 2017
There's no way to access this today. Is there any way you can avoid using a factory? Or just use a closed generic.
prasannapattam commentedon Jan 17, 2017
I can do a workaround with constructor.
I recommend enhancing the factory to support this functionality. I see this is a good feature.
davidfowl commentedon Jan 17, 2017
@prasannapattam The biggest problem is understanding of other containers can flow this context to their factory implementations.
prasannapattam commentedon Jan 17, 2017
I can see this is a problem on making this work for other containers.
Is it possible to create an overload which works for the default DI. Something like
khellang commentedon Jan 17, 2017
I think I've asked for this before (but maybe not filed and issue) in order to support decoration of open generics, so consider this my 👍
davidfowl commentedon Jan 20, 2017
@khellang @prasannapattam
Are you willing to do the research to see if all existing container adapters support this?
seesharper commentedon Jan 20, 2017
I really don't see how this can be supported without doing MakeGenericType somewhere in the factory implementation. That would not be all that efficient
Eilon commentedon Jan 31, 2017
Closing because we are not planning on adding this feature to the default DI container.