7

How do you achieve the following using Koin DI:

single { AValidator() } bind IValidator::class
single { BValidator() } bind IValidator::class
single { CValidator() } bind IValidator::class
single { DValidator() } bind IValidator::class

In the class where I want to have all validators injected I use the following:

val validators: List<IValidator> by inject()

Expecting that all different implementations of interface IValidator are injected automatically.

I know that is actually supported in Kodein, where you would simply do:

val validators: List<IValidator> by kodein.allInstances()

Would love to know whether this is possible within Koin.

Thanks!

M Ahayan
  • 121
  • 1
  • 5

3 Answers3

13

With Koin 2+, you can now declare your instances individually

single { AValidator() } bind IValidator::class
single { BValidator() } bind IValidator::class
single { CValidator() } bind IValidator::class
single { DValidator() } bind IValidator::class


Then use getAll<TInterface> to request all of them

val validators: List<IValidator> = getKoin().getAll()
// with lazy
val validators: List<IValidator> by lazy { getKoin().getAll<IValidator>() }


And use bind<TInterface, TImplementation> to request a single instance

val validator: IValidator = getKoin().bind<IValidator, AValidator>()


Source
https://start.insert-koin.io/#/getting-started/modules-definitions?id=additional-types

Asapha
  • 643
  • 8
  • 14
5

According to the documentation, I can do something like the following:

 single(name = "validators") {
        listOf(AValidator(), BValidator(), CValidator(), DValidator())
    }

And retrieve it with:

val validators: List<IValidator> by inject(name = "validators")

It works for now, but injecting a single validator of the list above would for example not work.

For more details: https://insert-koin.io/docs/1.0/documentation/reference/index.html

Feel free to add another solution!

M Ahayan
  • 121
  • 1
  • 5
  • @shkschneider The thing now is, that you cannot inject them individually, but only as a list of validators. I'll accept the answer for now. Hopefully Koin will support injecting a list of the same type in the near future. Thnx! – M Ahayan Feb 13 '19 at 21:33
  • @M Ahayan, there is much better answer avaliaveble which IMO should be the accepted one. Please consider it. – majkrzak Aug 21 '22 at 10:38
1

Built in Koin getAll() is also not working for me, regardless of version. So its either a bug that persisted for years or poor documentation. However this is my custom solution, koin version 3.3.0.

@OptIn(KoinInternalApi::class)
inline fun <reified T : Any> getAllCustom(): List<T> =
    KoinJavaComponent.getKoin().let { koin ->
        koin.instanceRegistry.instances.map { it.value.beanDefinition }
            .filter { it.kind == Kind.Singleton }
            .filter { it.primaryType.isSubclassOf(T::class) }
            .map { koin.get(clazz = it.primaryType, qualifier = null, parameters = null) }
    }

And then provide it like this

single { 
    SomeClass(getAllCustom()) 
}
hrabrica
  • 89
  • 3