CSC378 ยท 2081 ยท 5 marks
Describe the life cycle of Dependency Injection container.
Describe the life cycle of Dependency Injection container. [5]
Quick answer
NET Core, the built-in IoC (Inversion of Control) container manages the creation and lifetime of service instances. The lifetime of a service defines how long an instance lives and when a new instance is created.
Life Cycle of Dependency Injection (DI) Container
In ASP.NET Core, the built-in IoC (Inversion of Control) container manages the creation and lifetime of service instances. The lifetime of a service defines how long an instance lives and when a new instance is created. There are three main service lifetimes:
1. Transient
- The IoC container creates a new instance every time the service is requested.
- Each call to resolve the service produces a fresh object.
- Best suited for lightweight, stateless services.
Registration Example:
services.Add(new ServiceDescriptor(
typeof(ILog),
typeof(MyConsoleLogger),
ServiceLifetime.Transient));
Use case: A simple utility/helper service that holds no shared state.
2. Scoped
- The IoC container creates one instance per HTTP request and shares that same instance throughout the entire request.
- Different requests get different instances.
- Best suited for services that need to maintain state within a single request (e.g., database context).
Registration Example:
services.Add(new ServiceDescriptor(
typeof(ILog),
typeof(MyConsoleLogger),
ServiceLifetime.Scoped));
Use case: A database context or unit-of-work that should be consistent within one request.
3. Singleton
- The IoC container creates only one instance for the entire application lifetime.
- The same instance is reused for every request and every user.
- Best suited for services that are stateless and expensive to create, or that hold shared application-wide state.
Registration Example:
services.Add(new ServiceDescriptor(
typeof(ILog),
new MyConsoleLogger())); // single instance passed directly
Use case: A logging service or configuration service shared across the whole application.
Summary Table
| Lifetime | Instance Created | Shared Within |
|---|---|---|
| Transient | Every time requested | Not shared |
| Scoped | Once per HTTP request | Single request |
| Singleton | Once per application | Entire application |
How Registration Works
Services are registered inside the ConfigureServices() method in Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// Singleton
services.Add(new ServiceDescriptor(typeof(ILog), new MyConsoleLogger()));
// Transient
services.Add(new ServiceDescriptor(typeof(ILog),
typeof(MyConsoleLogger), ServiceLifetime.Transient));
// Scoped
services.Add(new ServiceDescriptor(typeof(ILog),
typeof(MyConsoleLogger), ServiceLifetime.Scoped));
}
Once registered, the IoC container automatically injects the service via constructor injection wherever the service type is used as a constructor parameter, without the developer needing to manually instantiate it.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.