Can we get a set/list/collection of instances base...
# koin
p
Can we get a set/list/collection of instances based in some interface? Eg.: getAll<Deeplink>()
🏃‍♂️ 1
c
a
yes
getAll()
can all request definitions bound to
T
type
p
It works only with Koin Annotations?
I looked the library, this code is inside in :core. So the answer is no. blob smile happy
a
exact ^^
p
I didn't tried it yet but I was thinking about it when I was driving...
Copy code
// 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().
I've implemented and validated. The override happens. Is it possible to achieve the return of DeeplinkOne and DeeplinkTwo using getAll()?
Copy code
// 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?
@arnaud.giuliani and @curioustechizen, I'm resurrecting the thread 😅.
a
Problem here https://kotlinlang.slack.com/archives/C67HDJZ2N/p1703013556766489?thread_ts=1702648414.130729&amp;cid=C67HDJZ2N is you are declaring 2 definitions with same root type. Then the last override the first. To have
getAll()
workable, best is to approach with this kind of definition:
Copy code
class DeeplinkOne : Deeplink()
class DeeplinkTwo : Deeplink()

// explicit binding
factory { DeeplinkOne() } bind Deeplink::class
factory { DeeplinkTwo() } bind Deeplink::class

// you have both here
getAll<Deeplink>()
p
You're right, using explicit binding it works! The explanation in docs are here. But why override was not applied to explicit
bind
if every dependency are binding with same interface? I've sure I understand the feature, but I don't get why this works! 😆
c
This should also with with
single
declarations, correct?
Copy code
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
	}
}
a
yep
c
@arnaud.giuliani Should this be documented? The fact the
binds
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.