BloomContainer class

Lightweight, high-performance Dependency Injection container for Bloom.

Architecture & Scoping Model

BloomContainer supports three binding lifecycles:

  1. Transient (provide): The factory is executed every time inject is called, producing a fresh instance each time.
  2. Singleton (provideSingleton): The factory is executed once, and the resulting instance is cached and reused across all subsequent inject calls. Can be created lazily on first resolution (default) or eagerly upon registration (lazy: false).
  3. Value (provideValue): An already-instantiated object is registered directly.

Hierarchical Resolution

Containers can be nested with an optional parent. When resolving a type T:

  1. The container checks for local test overrides (override / overrideType).
  2. The container checks its own local bindings.
  3. If not found locally and parent exists, resolution delegates up to the parent.
  4. If nowhere found, inject throws a descriptive StateError (or injectOrNull returns null).

Testing & Overrides

Test harnesses can register overrides via override or BloomTestScope that take precedence over existing bindings without altering production container setup.

Example

final container = BloomContainer();

// Register services
container.provideSingleton<DatabaseService>(() => PostgresService());
container.provide<UserRepository>(() => UserRepository(container.inject<DatabaseService>()));
container.provideValue<String>('https://api.example.com');

// Resolve dependencies
final userRepo = container.inject<UserRepository>();

Constructors

BloomContainer({BloomContainer? parent})
Creates a BloomContainer with an optional parent container.

Properties

hashCode int
The hash code for this object.
no setterinherited
parent BloomContainer?
Optional parent container for hierarchical dependency lookup cascades.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

dumpContainer() Map<String, dynamic>
Dumps container registrations, bindings, and active overrides for DevTools inspection.
has<T>() bool
Checks whether a dependency of type T is registered in this container, its overrides, or any ancestor parent container.
inject<T>() → T
Resolves the dependency of type T.
injectOrNull<T>() → T?
Resolves the dependency of type T, or returns null if not registered.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
override<T>(T instance) → void
Overrides dependency resolution for generic type T with a mock/stub instance.
overrideType(Type type, dynamic instance) → void
Overrides dependency resolution for an explicit runtime type with instance.
provide<T>(FactoryFunc<T> factory) → void
Registers a transient factory for type T.
provideSingleton<T>(FactoryFunc<T> factory, {bool lazy = true}) → void
Registers a singleton factory for type T.
provideValue<T>(T value) → void
Registers an existing value instance for type T.
removeOverride<T>([Type? type]) → void
Removes an active test override for type T (or explicitly specified type).
reset() → void
Clears all local bindings and active overrides from this container.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited