dependency_injection library

Provides a dependency injection container for managing services and their lifetimes.

This library implements the dependency injection pattern inspired by Microsoft.Extensions.DependencyInjection, enabling loose coupling and testability through constructor injection.

Service Lifetimes

Services can be registered with three different lifetimes:

  • Transient: A new instance is created each time it's requested
  • Scoped: One instance per scope (typically per request)
  • Singleton: A single instance shared across the application

Basic Usage

Register and resolve services:

final services = ServiceCollection()
  ..addSingleton<ILogger, ConsoleLogger>()
  ..addScoped<IDatabase, SqlDatabase>()
  ..addTransient<IEmailService, SmtpEmailService>();

final provider = services.buildServiceProvider();
final logger = provider.getRequiredService<ILogger>();

Keyed Services

Register multiple implementations with different keys:

services
  ..addKeyedSingleton<ICache, RedisCache>('redis')
  ..addKeyedSingleton<ICache, MemoryCache>('memory');

final redisCache = provider.getRequiredKeyedService<ICache>('redis');

Service Scopes

Create scoped services for request-scoped lifetimes:

await using((scope) async {
  final db = scope.serviceProvider.getRequiredService<IDatabase>();
  await db.saveChanges();
}, provider.createScope());

Classes

AsyncServiceScope
A ServiceScope implementation that implements AsyncDisposable.
DefaultServiceProviderFactory
Default implementation of ServiceProviderFactory<TContainerBuilder>.
ServiceCollection
Specifies the contract for a collection of service descriptors.
ServiceDescriptor
Describes a service with its service type, implementation, and lifetime.
ServiceProvider
Defines a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.
ServiceProviderFactory<TContainerBuilder>
Provides an extension point for creating a container specific builder and a ServiceProvider.
ServiceProviderIsKeyedService
Optional service used to determine if the specified type with the specified service key is available from the ServiceProvider.
ServiceProviderIsService
Optional service used to determine if the specified type is available from the ServiceProvider.
ServiceProviderOptions
Options for configuring various behaviors of the default ServiceProvider implementation.
ServiceScope
The dispose() method ends the scope lifetime. Once dispose is called, any scoped services that have been resolved from ServiceProvider will be disposed.
ServiceScopeFactory
A factory for creating instances of ServiceScope, which is used to create services within a scope.
SupportRequiredService
Optional contract used by ServiceProviderServiceExtensions.getRequiredService() to resolve services if supported by ServiceProvider.

Enums

ServiceLifetime
Specifies the lifetime of a service in a ServiceCollection.

Extensions

ServiceCollectionContainerBuilderExtensions on ServiceCollection
Extension methods for building a ServiceProvider from a ServiceCollection.
ServiceCollectionDescriptorExtensions on ServiceCollection
Extension methods for adding and removing services to an ServiceCollection.
ServiceCollectionServiceExtensions on ServiceCollection
Extension methods for adding services to a ServiceCollection.
ServiceProviderKeyedServiceExtensions on ServiceProvider
Extension methods for getting services from a ServiceProvider.
ServiceProviderServiceExtensions on ServiceProvider
Extension methods for getting services from a ServiceProvider.
ServiceScopeFactoryExtensions on ServiceScopeFactory
Extension methods for getting services from a ServiceScopeFactory.

Typedefs

ImplementationFactory = Object Function(ServiceProvider services)
A factory to create new instances of the service implementation.
KeyedImplementationFactory = Object Function(ServiceProvider services, Object? serviceKey)
TypedImplementationFactory<T> = T Function(ServiceProvider services)
TypedKeyedImplementationFactory<T> = T Function(ServiceProvider services, Object? serviceKey)