Marc
08/24/2022, 11:11 AMArkadii Ivanov
08/24/2022, 11:31 AMMarc
08/24/2022, 12:05 PMArkadii Ivanov
08/24/2022, 12:08 PMMarc
08/24/2022, 12:39 PMAdam Brown
08/24/2022, 7:38 PMAndrew Steinmetz
08/25/2022, 10:53 PMMarc
08/26/2022, 10:32 AMAndrew Steinmetz
08/26/2022, 5:13 PMclass 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.
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)
}