I'm trying to inject a generic interface but it do...
# koin
l
I'm trying to inject a generic interface but it doesn't work with the DSL style and bind():
Copy code
val appModule = module {
        singleOf(::MovieRepository)
        singleOf(::UpdateMovieUseCase) { bind<UseCase<List<MovieDomainModel>>>() }
        singleOf(::EditMovieUseCase) { bind<UseCase<EditMovieDomainModel>>() }
    }

    // This does not work
    val updateUseCase = koinInject<UseCase<List<MovieDomainModel>>>()
    // This works
    //val updateUseCase = koinInject<UpdateMovieUseCase>()
    val editUseCase = koinInject<UseCase<EditMovieDomainModel>>()

    println(updateUseCase)
    println(editUseCase)
Both will print
com.example.composetest.EditMovieUseCase@554e934
Can I achieve this with named qualifier? Issue: https://github.com/InsertKoinIO/koin/issues/1883
a
the bind() doesn't work then. And with the classic DSL, it should be better
l
@arnaud.giuliani what about this case:
Copy code
val appModule = module {
    single(named("Lol")) { UpdateMovieUseCase(get()) } withOptions {
        bind<UseCase<List<MovieDomainModel>>>()
    }
    single { MovieWrapper1(get(named("Lol"))) }
    single { MovieWrapper2(get(named("Lol"))) }
}
Copy code
class MovieWrapper1(private val movieUseCase: UseCase<List<MovieDomainModel>>) {}
class MovieWrapper2(private val movieUseCase: UseCase<List<MovieDomainModel>>) {}
When I inject
MovieWrapper1
and
MovieWrapper2
it creates two different instances instead of referencing the same. How can I achieve this?
a
it creates two different instances instead of referencing the same
yes your code is written like that
... oh wait
UpdateMovieUseCase
should be injected in both MovieWrapper1 MovieWrapper2?
you can activate logs to understand what's going on
l
@arnaud.giuliani thanks for helping, I found the problem on my side
👍 1