Skip to content

Support implicit value factory / Func<> generic factories in container #36387

@dotnetchris

Description

@dotnetchris

Is your feature request related to a problem? Please describe.

public class MyController : Controller
{
    public MyController(Func<MyItem> itemFactory)
    {
        Item = itemFactory.Invoke();

This should work out of box. However it only works if you manually register this dependecy ala

services.AddSingleton<Func<MyItem>>(provider => provider.GetService<MyItem>);

Describe the solution you'd like

To understand Func<> as a constructor argument means attempt to use the provider to create the dependency wrapped in a factory provider.

Describe alternatives you've considered

Manually creating the factories. It is not possible to create a generic factory due dotnet/extensions#478 not being implemented yet.

Additional context

https://stackoverflow.com/questions/35736070/how-to-use-funct-in-built-in-dependency-injection

This feature has been wanted for over 2 years.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions