Skip to content

Package level decoupling with FastAPI #797

Open
@ilterisYucel

Description

@ilterisYucel

version: 4.41.0
fastapi version: 0.110.2

In the project I developed with Fastapi, I want to use di as decoupled example at the package level.
I have OwnerContainer in owner package and i have ApplicationContainer in top level main package.

class OwnerContainer(containers.DeclarativeContainer):

    db_session = providers.Dependency()
    
    owner_service = providers.Factory(
        OwnerService,
        db_session=db_session,
    )

class ApplicationContainer(containers.DeclarativeContainer):
    config = providers.Configuration()
    database_session = providers.Resource(
        get_db_session
    )

    owner_package = providers.Container(
        OwnerContainer,
        db_session=database_session
    )

I want to use OwnerService in my owner router

@owner_router.get("/", response_model=List[GetOwnerSchema])
@inject
async def get_owners(service: OwnerService = Depends(Provide[OwnerContainer.owner_service])):
    try:
        result: List[GetOwnerSchema] = await service.get_owners()
        return result
    except Exception as e:
        print(e)
        raise HTTPException(status_code=500)

I got error 'Provide' object has no attribute 'get_owners'

According to my research, this is related to the dependency injector not being able to wire. I can't figure out whether this is related to a bug or whether I made a mistake, but I think I used it in accordance with the examples.
Has anyone encountered this type of problem? I need some help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions