Search code examples
kotlinkoin

Provide an Instance as its interface in Koin


Lets say I have two interfaces like:

interface LetterClassifier
interface NumberClassifier

Then these interfaces would be applied to this class:

class Classifier() : LetterClassifier, NumberClassifier

Now, I want to provide these instances only as LetterClassifier and NumberClassifier and not as Classifier in Koin.

The way I think of doing this is by doing:

module {
    val classifier = Classifier()

    single<NumberClassifier> { classifier }
    single<LetterClassifier> { classifier }
}

But I don't think this is the right way. Can someone guide me?


Solution

  • You could bind types to your definition like it is described on official article:

    single { Classifier() } binds arrayOf(LetterClassifier::class, NumberClassifier::class)
    

    If you want to exclude Classifier type at all you could do something like:

    single<LetterClassifier> { Classifier() } bind NumberClassifier::class