Inversion of Control IoC
--
Hello folks,
The framework doesn’t depend on you so you depend on the framework. That framework is Using Ioc logic. This method makes our work easier. IOC’s keywords are AddTransient, AddScoped and AddSingleton on .net core.
Definitions
AddSingleton
It is created once in the class to be used and The application stops until it is deleted from memory. It allows us to do all the operations on a single instance until the application stops working.
AddScoped
It is created on every request and only a new one is created. It’s a request-based method. So, It going to be created once per request.
AddTransient
It is created each anytime it is called. So, It is a method called every time, even inside request.
A quick sample, following;
In Startup.cs
public void ConfigureServices(IServiceCollection services){ services.AddSingleton<IDbClient, DbClient>(); services.AddTransient<IBaseServices, BaseServices>();}
In BaseController.cs
public class BaseController: ControllerBase{ private readonly IBaseServices _baseServices; public BaseController(IBaseServices baseServices) {
_baseServices = baseServices; }
}
Good luck.