davianspace_dependencyinjection library
A full-featured, enterprise-grade dependency injection container for Dart.
Equivalent to Microsoft.Extensions.DependencyInjection with modern
additions: keyed services (.NET 8 style), async service creation, scoped
providers, circular dependency detection, decorators, lazy resolution,
factory delegates, Options Pattern, service modules, and rich diagnostics.
Quick start
import 'package:davianspace_dependencyinjection/davianspace_dependencyinjection.dart';
// 1. Register services
final provider = ServiceCollection()
..addInstance<ILogger>(ConsoleLogger())
..addSingletonFactory<IDatabase>((p) => Database(p.getRequired<ILogger>()))
..addScoped<IUserRepository, UserRepository>()
..addTransient<IEmailSender, SmtpEmailSender>()
.buildServiceProvider(ServiceProviderOptions.development);
// 2. Resolve services
final db = provider.getRequired<IDatabase>();
// 3. Create a scope
final scope = provider.createScope();
final repo = scope.serviceProvider.getRequired<IUserRepository>();
scope.dispose();
// 4. Dispose root
provider.dispose();
Classes
- ActivatorHelper
- Global registry for ActivatorFactory entries.
- ActivatorUtilities
- Creates instances that receive a mix of DI-resolved and runtime arguments.
- DiagnosticEvent
- An event emitted by the DI container during build or resolve phases.
- KeyedServiceDescriptor
- A ServiceDescriptor that also carries a key for keyed-service lookup.
-
Lazy<
T extends Object> -
A wrapper that defers the creation of
Tuntil the first access. - ReflectionHelper
- Dart does not expose runtime reflection (mirrors) in AOT/non-development modes. Constructor injection therefore requires each implementation type to register its own factory function — a zero-overhead closure that receives a ServiceProviderBase-compatible resolver and creates the instance.
- ScopeManager
- Manages named ServiceScopeBase lifetimes outside the widget tree.
- ServiceCollection
- The mutable registry of service descriptors.
- ServiceDescriptor
- Describes a single service registration in the DI container.
-
ServiceFactory<
T extends Object> -
A factory that creates a new instance of
Ton each create call. - ServiceModule
- Base class for encapsulating a cohesive group of DI registrations.
- ServiceProvider
- The public-facing DI container.
- ServiceProviderBase
- Core resolution contract for the DI container.
- ServiceProviderDiagnostics
- Provides observable diagnostics for the DI container.
- ServiceProviderOptions
- Options controlling the behaviour of ServiceProvider at build time and during resolution.
- ServiceRegistrationInfo
- Describes a single service registration in the built container.
- ServiceScopeBase
- Represents a logical scope in the DI container.
- ServiceScopeFactoryBase
- Factory for creating ServiceScopeBase instances.
Enums
- DiagnosticSeverity
- Severity level of a DiagnosticEvent.
- ServiceLifetime
- The lifetime of a service registration.
Mixins
- AsyncDisposable
- A mixin for services that hold asynchronous resources and must be released when their owning scope or the root provider is disposed.
- Disposable
- A mixin for services that hold synchronous resources and must be released when their owning scope or the root provider is disposed.
Extensions
- ConfigurationServiceCollectionExtensions on ServiceCollection
-
Extension methods that register
davianspace_configurationtypes into ServiceCollection. - OptionsServiceCollectionExtensions on ServiceCollection
-
Extension methods that add the full
davianspace_optionsOptions Pattern to ServiceCollection.
Functions
-
createDefaultServiceFactory<
T extends Object> (ServiceProviderBase provider) → ServiceFactory< T> -
Creates a ServiceFactory<T> that resolves from
provider.
Typedefs
-
ActivatorFactory<
T extends Object> = T Function(Object resolve(Type dep), List< Object> positionalArgs) - A secondary factory signature used by ActivatorUtilities.
Exceptions / Errors
- CircularDependencyException
- Thrown when a circular dependency is detected during the build phase.
- ContainerBuildException
- Thrown when the container fails to build due to one or more registration or configuration errors.
- DisposalException
- Thrown when one or more services fail to dispose cleanly.
- MissingServiceException
- Thrown when the container cannot find a registration for a requested type.
- ScopeViolationException
- Thrown when a scoped service is captured by a singleton.