How should you (de-)compose multiple module dependencies #250
Replies: 2 comments 2 replies
-
After reading the source code a little bit more, I was hoping I'd be able to create some sort of utility function to compose multiple containers e.g.
container = Container()
# ...
container = Container()
# ...
container = Container()
compose(container, package_a.container.container, package_b.container.container But it's not obvious how to directly do this as a helper function without depending on the Another alternative would be to rely on a convention of having a
def configure(container): ...
# ...
def configure(container): ...
# ...
container = Container()
package_a.config.configure(container)
package_b.config.configure(container) A non Guice / Java example of a DI framework that provides this sort of modular configuration is |
Beta Was this translation helpful? Give feedback.
-
@strangemonad this is a really good question. I've not actually considered (de)composition of lagom in this way. How I've approach managing complexity is having separate containers for separate packages that extend a base container. So something like: # core/dependencies.py
base_dependency_container = Container()
base_dependency_container[Logger] = LogStuffClass
base_dependency_container[HttpClient] = SomeBaseHttpClient
#package_a/dependencies.py
container = Container(base_dependency_container)
container[SomeSpecialAThing] = lambda c: SomeSpecialAThing(c[HttpClient])
#package_b/dependencies.py
container = Container(base_dependency_container)
container[SomethingForB] = lambda c: SomethingForB(c[LogStuffClass]) Would this work for your use case? Otherwise we could definitely look at building a function to compose containers together. Although some thought would need to be given about what happens if two containers both define the same class. |
Beta Was this translation helpful? Give feedback.
-
First, thanks for this great library!
All of the examples show a single, top-level
Container
instance that you configure dependencies on. This is fine for small (and possibly even medium apps) but at some point, it's convenient to have re-usable dependency bindings e.g. an API client package might have the common bindings for real vs fake client implementations.In Guice you'd do this with a
Module
andbinder.install(otherModule)
. Similar in Dagger.This may be related to #200
Beta Was this translation helpful? Give feedback.
All reactions