https://kotlinlang.org logo
#decompose
Title
# decompose
p

Pavel S

01/31/2023, 8:41 PM
How is DI supposed to be organized in Decompose? Judging by the samples, the parent component creates the child components and passes everything via constructor, however I’m not sure about a couple of things: 1. What if we need dependencies that live longer than the component, say application-level? Using them as singletons or passing some kind of service-locator everywhere doesn’t seem too elegant. Are there any other solutions? 2. How can components be distributed to separate feature-modules without cross-feature dependencies? In samples projects either all components are located in shared/common module or a feature depends on other features?
a

Arkadii Ivanov

01/31/2023, 10:41 PM
1. The recommended way is to pass dependencies via constructors from top down to bottom. A dependency that leaves longer than a component has to be passed from the parent. A dependency that lives longer than the root component has to be passed from the integration point (e.g. an Activity). A service locator would also work, if you like it. 2. Dependencies between sibling modules are unnecessary. However parent modules are required to depend on child ones. You can also split each module by API and Impl, and let your parent modules depend only on API modules. In this case you will have to supply some kind of child component factories into impls, so that child components could be instantiated. But from my point of view this is an over-engineering. Check out the TodoApp sample - https://github.com/JetBrains/compose-jb/tree/master/examples/todoapp
10 Views