python-dependency-injector icon indicating copy to clipboard operation
python-dependency-injector copied to clipboard

Overriding in Provide

Open wackazong opened this issue 4 years ago • 0 comments

Hi there, I am slowly learning on all the intricacies of your package. It's sometimes to too easy to graps the concepts but it's fabulous how it all fits together.

One question: I use your package with FastAPI to inject external connections, gateways and usecases. One problem is the request-based session. I can acquire that in the controller which handles the request. In that controller I also receive the current usecase to execute via dependency injector like this:

@router.get('/')
async def get(
    usecase: Usecase = Depends(Provide[Application.usecase])
):
    ...

Now, ist there a possibility to inject the current session here like this:

class Usecase:
    def __init__(self, session: Session):
        ...


class Application(containers.DeclarativeContainer):
    usecase: Usecase = providers.Factory()


@router.get('/')
async def get(
    usecase: Usecase = Depends(Provide[Application.usecase.params(session=session)])
):
    ...

I hope you get the idea.

wackazong avatar Apr 16 '21 12:04 wackazong