Transient
Transient provider creates and returns a new object for each call. You can pass any callable object as the first parameter.
Also supports asynchronous dependencies.
Example
import asyncio
from dataclasses import dataclass
from typing import Tuple
from injection import DeclarativeContainer, providers
@dataclass
class SomeClass:
field: Tuple[int, int]
async def coroutine_func(arg1: int, arg2: int) -> Tuple[int, int]:
return arg1, arg2
class DIContainer(DeclarativeContainer):
coroutine = providers.Coroutine(coroutine_func, arg1=1, arg2=2)
sync_transient = providers.Transient(SomeClass, field=(10, 20))
async_transient = providers.Transient(SomeClass, field=coroutine)
async def main() -> None:
instance = await DIContainer.async_transient.async_resolve()
assert instance.field == (1, 2)
instance1 = DIContainer.sync_transient()
instance2 = DIContainer.sync_transient()
assert instance1 is not instance2
asyncio.run(main())