Dragos Rachieru
12/08/2023, 11:42 AMArkadii Ivanov
12/08/2023, 11:52 AMDennis
12/08/2023, 3:03 PMNacho Ruiz Martin
12/08/2023, 4:29 PMIam using Koin and works perfectly@Dennis Out of curiosity, could you make a quick summary on how you instantiate components? I don’t like the way I’m doing it.
Dennis
12/08/2023, 5:02 PMJohn O'Reilly
12/08/2023, 6:14 PMNacho Ruiz Martin
12/08/2023, 6:15 PMby inject
instead of injecting through the constructor, right?s3rius
12/08/2023, 7:56 PMKoin.get()
so that parents don't need to know the dependencies of their children.
I'm sailing fairly well with this. Simplified it's this:
// All Components are also KoinComponents
interface AppComponentContext : ComponentContext, KoinComponent
// Typesafe creator for new components
inline fun <reified Child : ComponentContext> AppComponentContext.getComponent(key: String) =
get<Child> { parametersOf(childContext(key)) }
class ParentComponent(
context: ComponentContext
) : AppComponentContext, ComponentContext by context {
// ...
// same works for ChildStack, etc
val child = getComponent<ChildComponent>(key = "child")
}
class ChildComponent(
context: ComponentContext,
otherDependency: WidgetFactory // injected by Koin
) : AppComponentContext, ComponentContext by context {
// ...
}
This simple example lacks a bunch of functionality. For example, it's not possible for the Parent to pass any dependencies explicitly which you probably want to do some time to time; but that can be added with some tricks.s3rius
12/08/2023, 7:59 PMArkadii Ivanov
12/08/2023, 8:01 PMArkadii Ivanov
12/08/2023, 8:02 PMArkadii Ivanov
12/08/2023, 8:02 PMDragos Rachieru
12/08/2023, 8:08 PMDragos Rachieru
12/08/2023, 8:09 PMDragos Rachieru
12/08/2023, 8:10 PMsubDI
for a child component
Using it this way, I can add a new dependency for a component and all of its childs, like a composition local