SpotContainer class

A scoped DI container that supports parent-child hierarchies.

SpotContainer allows creating isolated dependency scopes with fallback to parent containers. This is useful for:

  • Test isolation (override dependencies without affecting global state)
  • Feature-specific dependency trees
  • Request-scoped dependencies (e.g., in web applications)
  • Modular architecture with separate scopes per module

Hierarchy

Containers form a parent-child tree:

  • Global container (root, created by Spot)
  • Child scopes (created via createChild())
  • Grandchild scopes (children of children)

Resolution

When resolving a dependency, the container:

  1. Checks its own registry first
  2. Falls back to parent container if not found
  3. Continues up the chain until found or throws exception

Disposal

Disposing a container:

  • Only disposes services registered in that container
  • Does not affect parent or sibling containers
  • Calls Disposable.dispose() on services that implement it

Example

// Global dependencies
Spot.registerSingle<ISettings, Settings>((get) => Settings());

// Create test scope
final testScope = Spot.createScope();
testScope.registerSingle<ISettings, MockSettings>((get) => MockSettings());

// Use test scope
final settings = testScope.spot<ISettings>();  // Gets MockSettings

// Global scope unchanged
final globalSettings = spot<ISettings>();  // Gets Settings

// Cleanup test scope (doesn't affect global)
testScope.dispose();

See also:

  • Spot.createScope for creating child scopes
  • SpotTestHelper for test-specific scope utilities

Constructors

SpotContainer({SpotContainer? parent})
Creates a new container with optional parent.

Properties

hashCode int
The hash code for this object.
no setterinherited
log → Logger
Logger for this container
final
logging bool
Enable/disable logging for this container
getter/setter pair
parent SpotContainer?
Parent container for fallback resolution
final
registry Map<SpotKey, SpotService>
Local registry for this scope
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

createChild() SpotContainer
Create a child scope that inherits from this container.
dispose() → void
Dispose all services registered in this container only.
isRegistered<T>({String? name}) bool
Check if a type is registered in this container or any parent.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
registerAsync<T, R extends T>(SpotAsyncGetter<R> locator, {String? name}) → void
Register an async singleton with asynchronous initialization.
registerFactory<T, R extends T>(SpotGetter<R> locator, {String? name}) → void
Register a factory that creates a new instance on each resolution.
registerSingle<T, R extends T>(SpotGetter<R> locator, {String? name}) → void
Register a singleton that returns the same instance on each resolution.
spot<T>({String? name}) → T
Resolve and return an instance of type T.
spotAsync<T>({String? name}) Future<T>
Resolve and return an async singleton of type T.
toString() String
A string representation of this object.
inherited

Operators

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