Using `asp.net 5` I'd like my controller to be injected with a `Func<T>`instead of `T`

For example:

```cs
public HomeController(Func<Interfaces.IUnitOfWork> uow)

```

Instead of

```cs
public HomeController(Interfaces.IUnitOfWork uow)

```

Is it possible with the built-in DI or am I forced to move to an external DI?
['https://Stackoverflow.com/questions/35736070', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/20335/']
You can register a `Func<T>` or a delegate with a `ServiceCollection`. I recommend a delegate because it allows you to distinguish between different methods with identical signatures.

Here's an example. 

```
public interface IThingINeed {}
public class ThingINeed : IThingINeed { }

public delegate IThingINeed ThingINeedFactory();

public class DelegateRegistrationTests
{
    [Test]
    public void RegisterDelegateFromDependency()
    {
        var serviceCollection = new ServiceCollection();
        serviceCollection.AddTransient<IThingINeed, ThingINeed>();
        serviceCollection.AddTransient<ThingINeedFactory>(
            provider => provider.GetService<IThingINeed>);
        var serviceProvider = serviceCollection.BuildServiceProvider();
        var factoryMethod = serviceProvider.GetService<ThingINeedFactory>();
        var output = factoryMethod();
        Assert.IsInstanceOf<ThingINeed>(output);
    }
}

```

This almost looks like a service locator because the function we're resolving is actually `IServiceCollection.GetService<ThingINeedFactory>()`. But that's hidden in the composition root. A class that injects this delegate depends on the delegate, not on the implementation.

You can use the same approach if the method you want to return belongs to a class that the container must resolve. 

```
public interface IThingINeed
{
    string SayHello();
}

public class ThingINeed : IThingINeed
{
    private readonly string _greeting;

    public ThingINeed(string greeting)
    {
        _greeting = greeting;
    }

    public string SayHello() => _greeting;
}

public class ThingINeedFactory
{
    public IThingINeed Create(string input) => new ThingINeed(input);
}

public delegate IThingINeed ThingINeedFactoryMethod(string input);

public class DelegateRegistrationTests
{
    [Test]
    public void RegisterDelegateFromDependency()
    {
        var serviceCollection = new ServiceCollection();
        serviceCollection.AddSingleton<IThingINeed, ThingINeed>();
        serviceCollection.AddSingleton<ThingINeedFactory>();
        serviceCollection.AddSingleton<ThingINeedFactoryMethod>(provider => 
            provider.GetService<ThingINeedFactory>().Create);
        var serviceProvider = serviceCollection.BuildServiceProvider();
        var factoryMethod = serviceProvider.GetService<ThingINeedFactoryMethod>();
        var created = factoryMethod("abc");
        var greeting = created.SayHello();
        Assert.AreEqual("abc", greeting);
    }
}

```

Here's an extension method to (maybe) make it a little bit easier:

```
public static class ServiceCollectionExtensions
{
    public static IServiceCollection RegisterDelegate<TSource, TDelegate>(
        this IServiceCollection serviceCollection,
        Func<TSource, TDelegate> getDelegateFromSource) 
            where TDelegate : class 
    {
        return serviceCollection.AddSingleton(provider =>
            getDelegateFromSource(provider.GetService<TSource>()));
    }
}

serviceCollection
    .RegisterDelegate<ThingINeedFactory, ThingINeedFactoryMethod>(
        factory => factory.Create);

```
I have solution below

```cs
    public static IServiceCollection WithFunc<TService>(this IServiceCollection serviceCollection) where TService : class {
        var serviceType = typeof(TService);
        var serviceDescriptor = serviceCollection.LastOrDefault(x => x.ServiceType == serviceType);
        Debug.Assert(serviceDescriptor != null);
        serviceCollection.Add(ServiceDescriptor.Describe(typeof(Func<TService>),
            scope => new Func<TService>(scope.GetRequiredService<TService>),
            serviceDescriptor.Lifetime));
        return serviceCollection;
    }

```

usage

```cs
[Fact]
void with_func() {
    var services = new ServiceCollection()
        .AddTransient<IFoo, Foo>().WithFunc<IFoo>()
        .BuildServiceProvider();
    var fooFunc = services.GetRequiredService<Func<IFoo>>();
    Assert.NotNull(fooFunc);
}

```

more detail in gist <https://gist.github.com/leoninew/d2f174fe1422e453c60fb78e69342310>