Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does .net core dependency injection support Lazy<T>

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());     } } 
like image 375
ATerry Avatar asked Jul 05 '17 19:07

ATerry


People also ask

What dependency injection does .NET Core use?

ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. For more information specific to dependency injection within MVC controllers, see Dependency injection into controllers in ASP.NET Core.

What is lazy in .NET Core?

Lazy initialization of an object means that its creation is deferred until it is first used. (For this topic, the terms lazy initialization and lazy instantiation are synonymous.) Lazy initialization is primarily used to improve performance, avoid wasteful computation, and reduce program memory requirements.

How does dependency injection work .NET Core?

Dependency Injection is the design pattern that helps us to create an application which loosely coupled. This means that objects should only have those dependencies that are required to complete tasks.

How many types of dependency injection are there in ASP.NET Core?

NET Core provides three kinds of dependency injection, based in your lifetimes: Transient: services that will be created each time they are requested. Scoped: services that will be created once per client request (connection) Singleton: services that will be created only at the first time they are requested.


2 Answers

Here's another approach which supports generic registration of Lazy<T> so that any type can be resolved lazily.

services.AddTransient(typeof(Lazy<>), typeof(Lazier<>));  internal class Lazier<T> : Lazy<T> where T : class {     public Lazier(IServiceProvider provider)         : base(() => provider.GetRequiredService<T>())     {     } } 
like image 159
Michael Petito Avatar answered Sep 21 '22 00:09

Michael Petito


You only need to add a registration for a factory method that creates the Lazy<IRepo> object.

public void ConfigureService(IServiceCollection services) {     services.AddTransient<IRepo, Repo>();     services.AddTransient<Lazy<IRepo>>(provider => new Lazy<IRepo>(provider.GetService<IRepo>)); } 
like image 25
Scott Chamberlain Avatar answered Sep 23 '22 00:09

Scott Chamberlain