How to use Func <T> in the built-in dependency injection

Using asp.net 5 , I would like my controller to introduce Func<T> instead of T

For instance:

 public HomeController(Func<Interfaces.IUnitOfWork> uow) 

Instead

 public HomeController(Interfaces.IUnitOfWork uow) 

Is this possible with the built-in DI or am I forced to switch to an external DI?

+5
source share
2 answers

As far as I know, delaying such dependencies is impossible using the current default IoC container in ASP.NET Core. I still could not get it to work!

To defer initialization of dependencies like this, you need to implement an existing, more feature-rich IoC container.

+3
source

Func<T> not registered and is not allowed by default, but there is nothing that would prevent you from registering it yourself.

eg.

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

Please note that you also need to register IUnitOfWork as usual.

+2
source

Source: https://habr.com/ru/post/1244263/


All Articles