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

Marc

08/24/2022, 11:11 AM
👋 is anyone using decompose alongside something like koin or kodein?
a

Arkadii Ivanov

08/24/2022, 11:31 AM
A few days ago I had an experience of integrating Decompose into a multiplatform project - the official DroidconKotlin app. The project already uses Koin for DI. I'm not sure yet whether my PR is going to be merged or not, but you can check the branch - https://github.com/touchlab/DroidconKotlin/pull/165
m

Marc

08/24/2022, 12:05 PM
do you use scopes for anything?
a

Arkadii Ivanov

08/24/2022, 12:08 PM
I don't think so. DI was already there, I didn't notice any scopes.
m

Marc

08/24/2022, 12:39 PM
thanks!
a

Adam Brown

08/24/2022, 7:38 PM
I'm using it with Koin
I don't use scopes
a

Andrew Steinmetz

08/25/2022, 10:53 PM
I'm using it with Kodein in my own app and can't say that I have needed to use scopes as decompose will generally manage the scope of the dependency since it manages lifecycles
m

Marc

08/26/2022, 10:32 AM
i was thinking the same thing, so you are just making everything UI related a provider/factory and then let the LifecycleAware class handle the lifecycle etc right?
i guess if one really wants kodein to manage everything through scopes something like this would be trivial to implement. but as you said it’s probably not needed.
a

Andrew Steinmetz

08/26/2022, 5:13 PM
Yup, pretty much only using it for singleton and factories and inject everything through the constructor (technically could do the same thing with koin) and let decompose manage the lifecycle. So a sample would just look like:
Copy code
class FooBlocImpl(
    componentContext: ComponentContext, 
    private val bar: Bar,
    //
) : FooBloc, ComponentContext by componentContext {
    constructor(context: ComponentContext, di: DI) : this(
        componentContext = context, 
        bar = di.direct.instance() 
    )
}
So if
FooBlocImpl
is being added to the stack, all the necessary dependencies are created and decompose will not recreate another instance unless it was completely removed off the stack and pushed back on. Even the instance in my bottom navigation bloc, it uses the
bringToFront()
navigation method which will either create a new instance or bring a current one on the backstack to the front preventing it from creating multiple instances of the
FooBlocImpl
. I have considered moving to something like kotlin-inject using assisted injection for the
ComponentContext
of child blocs just for the compile time safety. Haven't really faced many problems with kodein though. In sample apps I have made, its also pretty easy to create your own DI service locator just using the basic language constructs in the kotlin language. Kodein and koin just solve a lot of problems that arise in more complicated DI setups.
Copy code
object DI {
    val foo = Foo() // eager singleton
    val bar by lazy { Bar() } // lazy singleton
    val authUseCase: AuthUseCase // factory 
        get() = AuthUseCaseImpl(foo)
    val createFizzUseCase(id: String): FizzUseCase = //assisted injection
      FizzUseCaseImpl(id, bar)
}
331 Views