A fundamental concept to understand when working with .Net Core's DI, is service lifetimes. A service lifetime defines how a dependency is managed in regards to how often it is created. As an illustration of this process, think of DI as managing a container of dependencies. Dependency is just a class that the DI knows about, because the class was registered with it. For .Net Core's DI, this is done with the following three methods of IServiceCollection:
- AddTransient<TService, TImplementation>()
- AddScoped<TService, TImplementation>()
- AddSingleton<TService, TImplementation>()
The IServiceCollection interface is a collection of registered service descriptions, basically containing the dependency, and when the DI should supply the dependency. For example, when TService is requested, TImplementation is supplied (that is, injected...