Lifetimes¶
Skirnir supports three service lifetimes, inspired by Microsoft.Extensions.DependencyInjection:
| Lifetime | Description |
|---|---|
| Singleton | Single instance per application. |
| Scoped | One instance per scope, reused within that scope. |
| Transient | New instance created each time the service is requested. |
Singleton¶
A singleton service is created once and shared across the entire application:
Scoped¶
Scoped services are created once per scope. Create a scope using CreateServiceScope():
auto scope = serviceProvider->CreateServiceScope();
auto scopedService = scope->GetServiceProvider()->GetService<MyScoped>();
Transient¶
A new instance is created each time the service is requested:
Choosing a Lifetime¶
- Use Singleton for stateless services or services that are expensive to create
- Use Scoped for services that should maintain state within a single request/scope
- Use Transient for lightweight, stateless services