Container-local factories #44
Replies: 2 comments 4 replies
-
Isn't that something that could be solved with nested services? If a factory takes an argument named So you can do something like: def tf(svcs_container) -> TenantConnection:
req = svcs_container.get(Request)
con = svcs_container.get(Connection)
con.execute(text("SET tenant = :id", {"id": req.tenant_id}))
return con If |
Beta Was this translation helpful? Give feedback.
-
The original problem has now been implemented in #56. I still plan on chaining registries and containers, but it's a bit of a separate problem because those, while more flexible, require to replace the container which is not always possible. Therefore I consider those two problems orthogonal. |
Beta Was this translation helpful? Give feedback.
-
Hej there!
We're using the litestar web framework. It comes with it's own DI system, which is not as ergonomic as svcs. One thing it can do though and which we are using extensively, is injecting dependencies on a per request basis. So something like adding an ephemeral dependency on the container, just for the request.
Maybe something like this:
Example use case with postgres/psycopg:
An HttpRequest has a "tenant_id" header value. That value gets passed into a factory that makes a TenantConnection from a Connection and sets some properties (e.g. calls a "SET tenant = :id"). Now all repos/services that require an injected Connection, use the TenantConection and the DB can do some additional checks on each SQL query. This works in litestar, because the Request is automatically available as a dependency for all downstream dependencies.
I hope my example is clear enough.
@hynek, what do you think of this? Could this be a feature you'd be willing to support?
Beta Was this translation helpful? Give feedback.
All reactions