Does .net core dependency injection support Lazy<T>

Viewed 22723

I am trying to use the generic Lazy class to instantiate a costly class with .net core dependency injection extension. I have registered the IRepo type, but I'm not sure what the registration of the Lazy class would look like or if it is even supported. As a workaround I have used this method http://mark-dot-net.blogspot.com/2009/08/lazy-loading-of-dependencies-in-unity.html

config:

public void ConfigureService(IServiceCollection services)
{
    services.AddTransient<IRepo, Repo>();
    //register lazy
}

controller:

public class ValuesController : Controller 
{
    private Lazy<IRepo> _repo;

    public ValuesController (Lazy<IRepo> repo)
    {
        _repo = repo;
    }

    [HttpGet()]
    public IActionResult Get()
    {
         //Do something cheap
         if(something)
             return Ok(something);
         else
             return Ok(repo.Value.Get());
    }
}
6 Answers

To my opinion, the code below should do the work(.net core 3.1)

services.AddTransient<IRepo, Repo>();
services.AddTransient(typeof(Lazy<>), typeof(Lazy<>));

Services that are to be fetched in Lazy will be re-introduced by the factory registration method with the new Lazy of the intended service type and provided for its implementation using serviceProvider.GetRequiredService.

services.AddTransient<IRepo, Repo>()
        .AddTransient(serviceProvider => new Lazy<IRepo>(() => serviceProvider.GetRequiredService<IRepo>()));

To register services as lazy

services.AddScoped<Lazy<AService>>();
services.AddScoped<Lazy<BService>>();

Or by creating an extension

static class LazyServiceCollection
{
    public static void AddLazyScoped<T>(this IServiceCollection services)
    {
        services.AddScoped<Lazy<T>>();
    }
}

...

services.AddLazyScoped<AService>();
services.AddLazyScoped<BService>();

And use it

[ApiController, Route("lazy")]
public class LazyController : ControllerBase
{
    private readonly Lazy<AService> _aService;
    private readonly Lazy<BService> _bService;

    public LazyController(Lazy<AService> aService, Lazy<BService> bService)
    {
        _aService = aService;
        _bService = bService;
    }

    [HttpGet("a")]
    public ActionResult GetA()
    {
        _aService.Value.DoWork();
        return new OkResult();
    }

    [HttpGet("b")]
    public ActionResult GetB()
    {
        _bService.Value.DoWork();
        return new OkResult();
    }
}

Result

Init AService
AService Work

I use this form, I hope resolve your problem, this code for Scoped and Transient Life-Cycle you can write for other Life-Cycle look like this.

My Dot-Net-Core vertion is 6

public static class Lazier
{
    public static IServiceCollection AddScopedLazier<T>(this IServiceCollection services)
    {
        return services.AddScoped(provider => new Lazy<T>(provider.GetService<T>));
    }
    public static IServiceCollection AddTransientLazier<T>(this IServiceCollection services)
    {
        return services.AddTransient(provider => new Lazy<T>(provider.GetService<T>));
    }
}

Usage for Scoped Life-Cycle:

services.AddScoped<ISideDal, EfSideDal>().AddScopedLazier<ISideDal>();

Usage for Transient Life-Cycle:

services.AddTransient<ISideDal, EfSideDal>().AddTransientLazier<ISideDal>();
Related