Wiring without a factory function per dependency #136
thatguysimon
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi! Thanks for modern-di and the rest of the modern-python libraries!
One thing that's holding me back from migrating to modern-di from that-depends is that many of my dependencies take plain values from a
settingsdependency, so each one ends up needing a factory function:In a real app that's one function per component, each one copying fields into a constructor. The dependency group stops showing the wiring, and the wiring was what drew me to
that-dependsandmodern-di.What I was hoping to write is something like:
But calling provider attributes this way doesn't work the way it does in that-depends.
In
litestar-sqlalchemy-templateI saw that settings aren't in the container at all:app/settings.pycreates a module-levelsettings = Settings(), which is imported directly.The snippet above already works if settings is that module-level instance, because kwargs accepts plain values. But then the values are fixed at import time, the environment has to be complete when the module is imported, and
container.override(Dependencies.settings, ...)no longer reaches them in tests. Keeping settings overridable is one of the reasons I want them in the container.Is keeping settings out of the container deliberate and the recommended pattern? If so, what's the reasoning, and how do you handle per-test settings with it?
Is there a pattern I'm missing that keeps both settings overridable and the group free of helper functions?
Thanks!
All reactions