Pedro Francisco de Sousa Neto
12/15/2023, 1:53 PMcurioustechizen
12/15/2023, 2:18 PMgetAll<T>()
that you can use for this:
https://insert-koin.io/docs/reference/koin-annotations/definitions#injecting-a-list-of-dependencies---listtarnaud.giuliani
12/15/2023, 2:40 PMgetAll()
can all request definitions bound to T
typePedro Francisco de Sousa Neto
12/15/2023, 3:07 PMPedro Francisco de Sousa Neto
12/15/2023, 3:11 PMarnaud.giuliani
12/15/2023, 3:31 PMPedro Francisco de Sousa Neto
12/19/2023, 7:01 PM// My classes
class DeeplinkOne : Deeplink()
class DeeplinkTwo : Deeplink()
// Declarations
factory { DeeplinkOne() }
factory { DeeplinkTwo() }
// Consumption
getAll<Deeplink>()
My getAll will return only DeeplinkTwo, because it has the same signature/interface for Deeplink right? I'm thinking this because if I'm not wrong, the Koin will override the DeeplinkOne() wtih DeeplinkTwo().Pedro Francisco de Sousa Neto
12/19/2023, 7:10 PMPedro Francisco de Sousa Neto
12/19/2023, 7:19 PM// My classes
class DeeplinkOne : Deeplink()
class DeeplinkTwo : Deeplink()
// Declarations
factory(named("a1")) { DeeplinkOne() }
factory(named("a2")) { DeeplinkTwo() }
// Consumption
getAll<Deeplink>()
I added a named("xyz") and worked for getAll(). But... that's not a good solution for my problem. Does anyone have a better one?Pedro Francisco de Sousa Neto
12/19/2023, 7:19 PMarnaud.giuliani
12/20/2023, 8:06 AMgetAll()
workable, best is to approach with this kind of definition:arnaud.giuliani
12/20/2023, 8:07 AMclass DeeplinkOne : Deeplink()
class DeeplinkTwo : Deeplink()
// explicit binding
factory { DeeplinkOne() } bind Deeplink::class
factory { DeeplinkTwo() } bind Deeplink::class
// you have both here
getAll<Deeplink>()
Pedro Francisco de Sousa Neto
12/20/2023, 11:50 AMbind
if every dependency are binding with same interface?
I've sure I understand the feature, but I don't get why this works! 😆curioustechizen
12/21/2023, 6:05 AMsingle
declarations, correct?
interface SignOutAware {
fun onSignOut()
}
interface FooRepository
interface BarRepository
class FooRepositoryImpl: FooRepository, SignOutAware
class BarRepositoryImpl: BarRepository, SignOutAware
class SignOutUseCase(signOutAwares: List<SignOutAware>)
val fooModule = module {
single { FooRepositoryImpl } binds arrayOf(FooRepository::class, SignOutAware::class)
}
val barModule = module {
single { BarRepositoryImpl } binds arrayOf(BarRepository::class, SignOutAware::class)
}
val appModule = module {
includes(fooModule, barModule)
factory {
SignOutUseCase(signOutAwares = getAll<SignOutAware>()) // Should return list containing both FooRepositoryImpl and BarRepositoryImpl
}
}
arnaud.giuliani
12/21/2023, 9:02 AMcurioustechizen
12/21/2023, 9:29 AMbinds
will cancel the default override
mechanism is not obvious.
This is also relevant outside the context of getAll()
. For example in the above case, if I was not using binds
then I would have expected by inject<SignOutAware>()
to returns BarRepositoryImpl
because of the override mechanism. But using binds
changes this.