Dependency Injection
Vorte includes a built-in dependency injection container that manages object creation, lifecycle, and wiring. It integrates seamlessly with FastAPI's Depends system while adding scopes, auto-wiring, and child containers.
Container
The Container is the central registry for all services and dependencies. Register services with a scope, and the container handles instantiation and lifecycle automatically.
Scopes
Scopes control how often a dependency is instantiated. Vorte supports three scopes:
| Scope | Lifetime | Use Case |
|---|---|---|
singleton | One instance for the entire application | Database pools, configuration, cache clients |
request | One instance per HTTP request | Request context, current user, unit of work |
transient | New instance on every resolution | DTOs, validators, stateless processors |
Singleton Scope
Request Scope
Transient Scope
@wire Decorator
Use @wire to auto-inject dependencies into class constructors. The container inspects type annotations and resolves them automatically.
Depends Integration
Vorte integrates with FastAPI's Depends for route-level dependency injection. Use container.depends to resolve services in route handlers.
@inject Decorator
Use @inject on functions that need dependency resolution outside of route handlers, such as background tasks or module initialization.
Child Containers
Create isolated child containers for modules or request scopes. Child containers inherit registrations from their parent but can override them locally.