-
-
Notifications
You must be signed in to change notification settings - Fork 305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Package level decoupling with FastAPI #797
Comments
Have a similar issue and can confirm, I want to make a library with some REST API routers for FastAPI application and with some DI bindings pre-configured: Here is the LoggignContainer where I have logging-related dependencies:
And here is a main BaseContainer where I aggregated all smaller containers from my library:
Here is my library router, very simple one and which uses DI from my lib, specifically my logger:
In a client code a client Container was defined, it has it's own application related dependencies + inherits from my BaseContainer (alternatively base container could be added via providers.Container(...) what is up to user):
Now client code need to register the library router (typical registration via FastAPI methods) + wire the router :
My expectations are Logger from my library will be injected in my router but the results is: I tried many combinations and changed code many ways but unless I directly use AppContainer in my library code (what is impossible to expect beyond local examples and testing since I can not know what is App level container in advance) wiring doesn't work I also tried to make router initialization based on the factory method, this way I provided instance of the AppContainer which was inherited from BaseContainer:
So client code could provide me actual container, and in debug mode I have seen that instance is right one and it has all providers inside. However results was the same: "AttributeError: 'Provide' object has no attribute logger" Is there any solution for this issue? |
Try setting class Container(containers.DeclarativeContainer):
wiring_config = containers.WiringConfiguration(packages=["yourapp.routers"]) |
Thank you for your response. I upgrded my python version 3.12, when library is upgraded for python 3.12, i will try your solution. |
A release for python 3.12 is now available |
@ilterisYucel You can not have 2 global container at same time.
|
You have 2 container: BaseContainer for global scope , LoggingContainer for local scope You can call the LoggingContainer like below, ( wiring_config including router.py also needed on BaseContainer )
|
Thank you for your suggestion. I will try as you suggested. |
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.
I want to use OwnerService in my owner router
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.
The text was updated successfully, but these errors were encountered: