Skip to content
This repository was archived by the owner on Nov 2, 2018. It is now read-only.
This repository was archived by the owner on Nov 2, 2018. It is now read-only.

implementationFactory for Open Generics #474

Closed

Description

@prasannapattam

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 => new ServiceDocument<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?

Activity

  1. davidfowl commented on Jan 17, 2017

    @davidfowl
    Member

    There's no way to access this today. Is there any way you can avoid using a factory? Or just use a closed generic.

  2. prasannapattam commented on Jan 17, 2017

    @prasannapattam
    Author

    I can do a workaround with constructor.

    I recommend enhancing the factory to support this functionality. I see this is a good feature.

  3. davidfowl commented on Jan 17, 2017

    @davidfowl
    Member

    @prasannapattam The biggest problem is understanding of other containers can flow this context to their factory implementations.

  4. prasannapattam commented on Jan 17, 2017

    @prasannapattam
    Author

    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

    Func<IServiceProvider, Type, object> implementationFactory
    
  5. khellang commented on Jan 17, 2017

    @khellang
    Contributor

    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 👍

  6. davidfowl commented on Jan 20, 2017

    @davidfowl
    Member

    @khellang @prasannapattam

    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 👍

    Are you willing to do the research to see if all existing container adapters support this?

  7. seesharper commented on Jan 20, 2017

    @seesharper
    Member

    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

  8. Eilon commented on Jan 31, 2017

    @Eilon
    Contributor

    Closing because we are not planning on adding this feature to the default DI container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions