BloomContainer class
Lightweight, high-performance Dependency Injection container for Bloom.
Architecture & Scoping Model
BloomContainer supports three binding lifecycles:
- Transient (provide): The factory is executed every time inject is called, producing a fresh instance each time.
- 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). - Value (provideValue): An already-instantiated object is registered directly.
Hierarchical Resolution
Containers can be nested with an optional parent. When resolving a type T:
- The container checks for local test overrides (override / overrideType).
- The container checks its own local bindings.
- If not found locally and parent exists, resolution delegates up to the parent.
- 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
parentcontainer.
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
Tis 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 returnsnullif 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
Twith a mock/stubinstance. -
overrideType(
Type type, dynamic instance) → void -
Overrides dependency resolution for an explicit runtime
typewithinstance. -
provide<
T> (FactoryFunc< T> factory) → void -
Registers a transient
factoryfor typeT. -
provideSingleton<
T> (FactoryFunc< T> factory, {bool lazy = true}) → void -
Registers a singleton
factoryfor typeT. -
provideValue<
T> (T value) → void -
Registers an existing
valueinstance for typeT. -
removeOverride<
T> ([Type? type]) → void -
Removes an active test override for type
T(or explicitly specifiedtype). -
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