Koin: Koin 2.0.0 — Type inference and names

Created on 9 Dec 2018  Â·  5Comments  Â·  Source: InsertKoinIO/koin

Describe the bug:
I tried to define various Repository pattern components, naming my singles to base my DI on interfaces only, as in:

// Repository
single<IFirstDataSource>(name = "local") {
    FirstLocalDataSource.getInstance(get())
}
single<IFirstDataSource>(name = "remote") {
    FirstRemoteDataSource.getInstance(get())
}
single<IFirstDataSource>(name = "repository") {
    FirstRepository.getInstance(get("local"), get("remote"))
}
...
// Repository
single<ISecondDataSource>(name = "local") {
    SecondLocalDataSource.getInstance(get())
}
single<ISecondDataSource>(name = "remote") {
    SecondRemoteDataSource.getInstance(get())
}
single<ISecondDataSource>(name = "repository") {
    SecondRepository.getInstance(get("local"), get("remote"))
}

Then I met a DefinitionOverrideException:

org.koin.core.error.DefinitionOverrideException: Already existing definition or try to override an existing one with name 'local' with Single[name:'local', type:'fr.sample.app.second.data.ISecondDataSource'] but has already registered Single[name:'local', type:'fr.sample.app.first.data.IFirstDataSource']

Expected behavior:
I was expecting Kotlin type inference to precede the name while resolving the dependency.

Because the first single(name = "local") is a IFirstDataSource and the second a ISecondDataSource, I was expecting the type to be part of the definition so a conflict can only occur if I use a name twice for a unique type, as in:

single<IFirstDataSource>(name = "local") {
    ...
}
single<IFirstDataSource>(name = "local") {
    ...
}

Koin project used and used version:

def koin_version = "2.0.0-alpha-4"
implementation "org.koin:koin-android:$koin_version"
implementation "org.koin:koin-androidx-scope:$koin_version"
implementation "org.koin:koin-androidx-viewmodel:$koin_version"
testImplementation "org.koin:koin-test:$koin_version"
androidTestImplementation "org.koin:koin-test:$koin_version"

Question:

Is this a bug or the planned behavior and I must change the names?

question

All 5 comments

I think you can make use of custom namespaces in order to distinguish between different objects. Koin doesn't understand generics well.
Refer here https://insert-koin.io/docs/1.0/documentation/koin-core/#_modules_namespaces

@nikhil-thakkar
Am I missing something when believing that a Module can not be named in Koin 2.0.0?

Because in 2.0.0 we are getting back on namespace & visibility rules that are complex. If fact to ensure visibility you can do it at your Kotlin module level, not need Koin to simulate this kind of separation.

For generics, we lose the generic inferred type. We have the type but not the generic argument because we are working with the KClass met info of you inferred type.

I think you can make use of custom namespaces in order to distinguish between different objects. Koin doesn't understand generics well.
Refer here https://insert-koin.io/docs/1.0/documentation/koin-core/#_modules_namespaces

Page not found :(
did you mean: https://insert-koin.io/docs/2.0/reference-documentation/koin-core/modules.html ?

more around here yeah

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jeevuz picture Jeevuz  Â·  4Comments

iRYO400 picture iRYO400  Â·  3Comments

mubarak1361 picture mubarak1361  Â·  3Comments

CristianMG picture CristianMG  Â·  3Comments

sankarsana picture sankarsana  Â·  4Comments