modern-python / that-depends

DI-framework, inspired by python-dependency-injector, but without wiring. Python 3.12 is supported
https://that-depends.readthedocs.io/
MIT License
156 stars 12 forks source link

Migration guide from dependency_injector #60

Closed JobaDiniz closed 2 months ago

JobaDiniz commented 2 months ago
  1. It's written that this lib does not have wiring. What's the alternative and why it does not matter? What do I need to change?
  2. I'm using dead dependency_injector for fastapi. Does that-depends support passing the request: Request into the dependency?

https://that-depends.readthedocs.io/introduction/fastapi.html

@app.get("/")
async def read_root(
    some_dependency: typing.Annotated[
        container.DependentFactory,
        fastapi.Depends(container.DIContainer.dependent_factory),
    ],
) -> datetime.datetime:
    return some_dependency.async_resource

In the above example, I need that the instance of dependent_factory to have access to the request object of fastapi. How can this be implemented?

lesnik512 commented 2 months ago

@JobaDiniz Hi,

  1. The alternative is that there is no container initialization and you can't have several instances of the same container. the main advantage is that you don't need to write such boilerplate code:
    container = Container()
    container.wire(modules=[__name__])
lesnik512 commented 2 months ago

@JobaDiniz

  1. can you please show how you solve this case in dependency injector?
JobaDiniz commented 2 months ago

I haven't solved.

lesnik512 commented 2 months ago
  1. there is only one clean solution I can think off:
@app.get("/")
async def read_root(request: Request) -> datetime.datetime:
    # and here build dependency and pass data from request to needed methods
    some_object = await container.DIContainer.dependent_factory()
    some_object.some_method(request)