Providers¶
Providers describe how to create an instance of a type. Each variant is a separate class — the type checker enforces correct usage.
ClassProvider¶
Map an interface to a concrete class. The container auto-wires the class's __init__.
FactoryProvider¶
Provide a callable that receives the container. Use for conditional logic or complex setup.
FactoryProvider(
provide=BillingGateway,
use_factory=lambda c: StripeBillingGateway(c.resolve(Config)),
)
ValueProvider¶
Register a pre-built instance.
ExistingProvider¶
Alias one type to another. When provide is requested, the container resolves use_existing instead.
Eliminates boilerplate factory functions for port → implementation mappings.
Scope¶
ClassProvider and FactoryProvider accept a scope parameter. Default is Scope.SINGLETON.
| Scope | Behavior |
|---|---|
SINGLETON (default) |
One instance per container |
TRANSIENT |
New instance every resolve() |
Union Type¶
All four are unified under Provider:
Bare Type Shorthand¶
Pass a class directly in providers as shorthand for ClassProvider(provide=T, use_class=T):
In NestJS, this is equivalent to listing a class in
providers: [TeamService].