Do Koin support something like "Get all implementa...
# koin
l
Do Koin support something like "Get all implementations of this class"
r
Can you please be specific.
l
I think what I'm doing is more of a code smell
Given I have the interface
Foo
and the classes
Boo : Foo
and
Bat : Foo
, is there a way to
get<Foo>()
and obtain both
Boo
and
Bat
?
r
one interface, multiple implementation. You can use named qualifier and can the same.
l
But I don't want to specify, I was going to test "All repositories do this", and this would be future proof if new repositories show up (very likely in my context)
m
Hmm, the interface should do fine *imho. Every new repo that implement that interface, you can do something like this:
Copy code
single { NewRepoImpl() }
single { ViewModelNeedRepository(get<NewRepoImpl>() as Foo) }
but looks like if you mean you just want to provide a
Foo
interface, and don't want to provide
NewRepo()
, I think that won't be possible currently? @LeoColman
l
Yes. what I tried to say is that, like:
Copy code
single { Boo() }
single { Bat() }

...
val foos: List<Foo> = getAll<Foo>()
I believe I might be overengineering this, but I wanted to test that all my repositories could provide the same data
I was creating a test to see that, for example, the H2 and MySql implementations worked the same way
I could write a test for both of them, sure, but I wanted a more generic approach, as I have use cases that would test 6~8 of implementations of the same interface, and I was trying to avoid the boilerplate
I hope I made myself clear, i think I'm being a little bit confusing here xD
The repositories was just an example. My actual use case is data generation through different algorithms. In the end their answer must be the same
m
I see, in your case, you might wanna try to open an issue on the koin repo. or idk this would solve your issue or not: https://insert-koin.io/docs/2.0/documentation/reference/index.html go to the section: 3.10.2. Dealing with generics
Copy code
single { ArrayList<Foo> }
g
Hi @LeoColman, in case you want to inject an implementation of a interface i leave you here what i’m actually doing ->
Copy code
single<MyRepositoryInterface>(named("REPOSITORY")) { MyRepositoryImpl(get(named("MyRepositoryDependency"))) }
you can use the
getAll<YourType>
from koin instance
m
Awesome works @arnaud.giuliani
👍 1