mori-atsushi / koject

DI Container library for Kotlin Multiplatform.
https://mori-atsushi.github.io/koject/
Apache License 2.0
122 stars 3 forks source link

Missing support for multibinds #250

Open kaserdan opened 1 year ago

kaserdan commented 1 year ago

Dagger allows you to bind several objects into a collection even when the objects are bound in different modules using multibindings. Similar thing is possible with Koin. Is there any way to binds object into collection in Koject?

mori-atsushi commented 1 year ago

There is not multibinds feature in Koject.

I have never used that feature, so I do not know why it is necessary. If you tell me the use case you imagine, I will consider it.

kaserdan commented 1 year ago

We have multi module project (with multiple apps) where some modules expose ktor interceptor. Then some other module can "collect" these interceptors without having dependecy on them, so networking module can get these interceptor from "top" - base on application being build.

mori-atsushi commented 1 year ago

I'm not sure about ktor, but I think it works like this:

// common module
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class Interceptor1

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class Interceptor2
// lib module1
@Provides
@Interceptor1
fun provideInterceptor1(): Interceptor {
    /* ... */
}
// lib module2
@Provides
@Interceptor2
fun provideInterceptor2(): Interceptor {
    /* ... */
}
// app module
@Provides
fun provideInterceptorSet(
    @Interceptor1
    interceptor1: Interceptor,
    @Interceptor2
    interceptor2: Interceptor
): Set<Interceptor> {
    return setOf(interceptor1, interceptor2)
}
// networking module
@Provides
class SameClass(
    private val providerSet: Set<Provider>
) {
    /* ... */
}

If there is a IntoSet annotation like dagger, we can remove the provider code in the app module. But it may be hard to know what will be used.