Closed Maksim-Burtsev closed 7 months ago
I found the way to make it work
I hope this will help those who face the same error:
container.py
from dependency_injector import containers, providers
from dependency_injector import containers, providers
from sqlalchemy import orm
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from infrastructure.repositories import *
from services import *
class Container(containers.DeclarativeContainer):
# db
engine = providers.Singleton(
create_async_engine,
DATABASE_URL,
pool_size=100,
)
async_session = providers.Factory(
orm.sessionmaker,
bind=engine,
class_=AsyncSession,
expire_on_commit=False,
)
# repositories
foo_repo = providers.Factory(
FooRepository,
session=async_session,
)
...
#services
foo_service = providers.Singleton(
FooService,
repo=foo_repo,
)
...
with app creation:
app.container: Container = Container()
app.container.wire(
[
"routers.foo_router",
...
]
and now everything works in routers/foo_router.py
:
@router.get(
"/",
response_model=list[FooSchema],
)
@inject
async def get_foos(
foo_service: FooService = Depends(Provide[Container.foo_service]),
) -> list[FooModel]:
return await foo_service.get_objects()
I use https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/fastapi-sqlalchemy as example for use DI in project with fastAPI + sqlalchemy (async), but when i try to start webapp i have this error:
I have some differences instead of example:
my
database.py
my
container.py
:and with app creation i have
What this error means and how can i fix it?
(python 3.11)