Is That-Depends could be used in a Library? #51
-
Hey mates, To elaborate a bit more about my particular example (which fails with other DI libraries): I want to make a library with some REST API routers for FastAPI application and with some DI bindings to pre-configured so my routers will receive my services: 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, it then can be used in the client code to register (for me) and access (for client) my services if needed:
Here is my library router which should automate some work, very simple one and which uses services registred in IoC Contaier from my lib, specifically my logger:
In a client code some steps should be done, firstly a client Container to be defined, it has it's own application-related dependencies + inherits from my BaseContainer:
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 So now I started to check you examples and in the FastAPI example I see such lines of code:
Where ioc.IOCContainer.decks_repo is a reference to the container in the application code and I decided to ask in advance. Is it possible in your lib to:
Thank you 🙂 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
@rustam-ashurov-mcx Seems like you doing something like this https://python-dependency-injector.ets-labs.org/examples/application-multiple-containers.html. |
Beta Was this translation helpful? Give feedback.
@rustam-ashurov-mcx Sorry for late response.
I would describe library's class in ioc-container and pass needed dependencies inside as arguments.
And I would use protocols to describe required dependencies