ServiceContainer
Bases: ServiceCollection, AbstractAsyncContextManager['ServiceContainer']
Collection of resolvable services.
add_auto_activated_keyed_singleton
add_auto_activated_keyed_singleton(
service_key: TKey | None, service_type: type[TService]
) -> None
add_auto_activated_keyed_singleton(
service_key: TKey | None,
service_type: type[TService],
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
AsyncGenerator[TService],
],
) -> None
add_auto_activated_keyed_singleton(
service_key: TKey | None,
service_type: type[TService],
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
Generator[TService],
],
) -> None
add_auto_activated_keyed_singleton(
service_key: TKey | None,
service_type: type[TService],
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
Awaitable[TService],
],
) -> None
add_auto_activated_keyed_singleton(
service_key: TKey | None,
service_type: type[TService],
implementation_factory: Callable[
[TKey | None, *TFactoryParameter], TService
],
) -> None
add_auto_activated_keyed_singleton(
service_key: TKey | None,
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
AsyncGenerator[TService],
],
) -> None
add_auto_activated_keyed_singleton(
service_key: TKey | None,
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
Generator[TService],
],
) -> None
add_auto_activated_keyed_singleton(
service_key: TKey | None,
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
Awaitable[TService],
],
) -> None
add_auto_activated_keyed_singleton(
service_key: TKey | None,
implementation_factory: Callable[
[TKey | None, *TFactoryParameter], TService
],
) -> None
Add an auto-activated keyed singleton service.
An auto-activated keyed singleton service is instantiated when the service provider is built (eagerly), rather than when it's first requested (lazily).
add_auto_activated_singleton
add_auto_activated_singleton(
service_type: type[TService],
implementation_factory: Callable[
..., AsyncGenerator[TService]
],
) -> None
add_auto_activated_singleton(
service_type: type[TService],
implementation_factory: Callable[
..., Generator[TService]
],
) -> None
add_auto_activated_singleton(
service_type: type[TService],
implementation_factory: Callable[
..., Awaitable[TService]
],
) -> None
add_auto_activated_singleton(
service_type: type[TService],
implementation_factory: Callable[..., TService],
) -> None
add_auto_activated_singleton(
implementation_factory: Callable[
..., AsyncGenerator[TService]
],
) -> None
add_auto_activated_singleton(
implementation_factory: Callable[
..., Generator[TService]
],
) -> None
Add an auto-activated singleton service.
An auto-activated singleton service is instantiated when the service provider is built (eagerly), rather than when it's first requested (lazily).
add_keyed_scoped
add_keyed_scoped(
service_key: TKey | None,
service_type: type[TService],
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
AsyncGenerator[TService],
],
) -> None
add_keyed_scoped(
service_key: TKey | None,
service_type: type[TService],
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
Generator[TService],
],
) -> None
add_keyed_scoped(
service_key: TKey | None,
service_type: type[TService],
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
Awaitable[TService],
],
) -> None
add_keyed_scoped(
service_key: TKey | None,
service_type: type[TService],
implementation_factory: Callable[
[TKey | None, *TFactoryParameter], TService
],
) -> None
add_keyed_scoped(
service_key: TKey | None,
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
AsyncGenerator[TService],
],
) -> None
add_keyed_scoped(
service_key: TKey | None,
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
Generator[TService],
],
) -> None
add_keyed_scoped(
service_key: TKey | None,
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
Awaitable[TService],
],
) -> None
Add a keyed scoped service.
add_keyed_singleton
add_keyed_singleton(
service_key: TKey | None,
service_type: type[TService],
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
AsyncGenerator[TService],
],
) -> None
add_keyed_singleton(
service_key: TKey | None,
service_type: type[TService],
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
Generator[TService],
],
) -> None
add_keyed_singleton(
service_key: TKey | None,
service_type: type[TService],
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
Awaitable[TService],
],
) -> None
add_keyed_singleton(
service_key: TKey | None,
service_type: type[TService],
implementation_factory: Callable[
[TKey | None, *TFactoryParameter], TService
],
) -> None
add_keyed_singleton(
service_key: TKey | None,
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
AsyncGenerator[TService],
],
) -> None
add_keyed_singleton(
service_key: TKey | None,
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
Generator[TService],
],
) -> None
add_keyed_singleton(
service_key: TKey | None,
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
Awaitable[TService],
],
) -> None
add_keyed_singleton(
service_key: TKey | None,
implementation_factory: Callable[
[TKey | None, *TFactoryParameter], TService
],
) -> None
Add a keyed singleton service.
add_keyed_transient
add_keyed_transient(
service_key: TKey | None,
service_type: type[TService],
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
AsyncGenerator[TService],
],
) -> None
add_keyed_transient(
service_key: TKey | None,
service_type: type[TService],
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
Generator[TService],
],
) -> None
add_keyed_transient(
service_key: TKey | None,
service_type: type[TService],
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
Awaitable[TService],
],
) -> None
add_keyed_transient(
service_key: TKey | None,
service_type: type[TService],
implementation_factory: Callable[
[TKey | None, *TFactoryParameter], TService
],
) -> None
add_keyed_transient(
service_key: TKey | None,
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
AsyncGenerator[TService],
],
) -> None
add_keyed_transient(
service_key: TKey | None,
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
Generator[TService],
],
) -> None
add_keyed_transient(
service_key: TKey | None,
implementation_factory: Callable[
[TKey | None, *TFactoryParameter],
Awaitable[TService],
],
) -> None
Add a keyed transient service.
add_scoped
add_scoped(
service_type: type[TService],
implementation_factory: Callable[
..., AsyncGenerator[TService]
],
) -> None
add_scoped(
service_type: type[TService],
implementation_factory: Callable[
..., Generator[TService]
],
) -> None
add_scoped(
service_type: type[TService],
implementation_factory: Callable[
..., Awaitable[TService]
],
) -> None
Add a scoped service.
add_singleton
add_singleton(
service_type: type[TService],
implementation_factory: Callable[
..., AsyncGenerator[TService]
],
) -> None
add_singleton(
service_type: type[TService],
implementation_factory: Callable[
..., Generator[TService]
],
) -> None
add_singleton(
service_type: type[TService],
implementation_factory: Callable[
..., Awaitable[TService]
],
) -> None
Add a singleton service.
add_sync_sqlmodel
Add synchronous SQLModel services.
add_transient
add_transient(
service_type: type[TService],
implementation_factory: Callable[
..., AsyncGenerator[TService]
],
) -> None
add_transient(
service_type: type[TService],
implementation_factory: Callable[
..., Generator[TService]
],
) -> None
add_transient(
service_type: type[TService],
implementation_factory: Callable[
..., Awaitable[TService]
],
) -> None
Add a transient service.
build_service_provider
build_service_provider(
validate_scopes: bool = False,
validate_on_build: bool = True,
) -> ServiceProvider
Create a :class:ServiceProvider containing services from the this :class:ServiceContainer.
configure_fastapi
Configure the FastAPI application to use dependency injection using the services from this service container.
create_scope
Create a new :class:ServiceScope that can be used to resolve scoped services.
enable_keyed_singleton_auto_activation
Mark a registered keyed singleton service as auto-activated.
An auto-activated keyed singleton service is instantiated when the service provider is built (eagerly), rather than when it's first requested (lazily).
enable_singleton_auto_activation
Mark a registered singleton service as auto-activated.
An auto-activated singleton service is instantiated when the service provider is built (eagerly), rather than when it's first requested (lazily).
get
async
Get service of type TService or raise :class:NoServiceRegisteredError.
get_all
async
Get all services of type TService.
get_all_keyed
async
Get all services of type TService.
get_keyed
async
Get service of type TService or raise an error.
override
Override a service registration within the context manager scope.
It can be used to temporarily replace a service for testing specific scenarios. Don't use it in production.
override_keyed
override_keyed(
service_key: object | None,
service_type: type,
implementation_instance: object | None,
) -> Generator[None]
Override a keyed service registration within the context manager scope.
It can be used to temporarily replace a service for testing specific scenarios. Don't use it in production.
try_get
async
Get service of type TService or return None.