Describe the bug
Coming from Dagger and migrating our app to Koin I've found a scenario that doesn't seem to work with Koin. I'm not sure if it's intended or it's a bug, I didn't find anything about it in the documentation.
Koin allows us to have multiple definitions of the same type by setting a name. But it fails when we have both named and not-named definitions. Here's a simple test which reproduces the issue:
To Reproduce
@Test
fun named_vs_default() {
startKoin(listOf(
module {
single(name = "Foo") { MyDependency() }
single { MyDependency() }
}
))
get<MyDependency>(name = "Foo") // Works fine
get<MyDependency>() // Error
}
The error produced when resolving the dependency without name is:
Error while resolving instance for class '[...].MyDependency' - error: org.koin.error.DependencyResolutionException: Multiple definitions found for type 'class [...]Test$MyDependency' - Koin can't choose between :
Single [name='Foo',class='[...].MyDependency']
Single [name='MyDependency',class='[...].MyDependency']
Check your modules definition, use inner modules visibility or definition names.
Expected behavior
I would a behavior similar to Dagger's: When requesting the dependency without name, it should use the default definition.
If this is an intended behavior, I would expect Koin to not accept the definition and fail when loading the modules. Or at least be mentioned in the docs.
Koin project used and used version :
koin-android version 1.0.1
As a workaround we just added a "default" name to the default dependencies we had.
The second one get<MyDependency>(), is a global lookup about your dependency. Then you should use the default name for this component (I think it should be MyDependency, check your logs).
Yes, according to the logs Koin is assigning "MyDependency" as the default name, so I need to request the instance passing that String as the name.
But from my point of view that's inconsistent with the definition. Since I defined no name in the declaration, I would expect to not use any name for resolving it either. Right?
Yep, having the same issue, having to specifically add a "default" name to every module declarations and dependency lookups (get, inject) just to avoid conflict with the root namespace is kinda annoying.
Resolved in next 1.1 that come back to simpler modules & binding like in your case.
Most helpful comment
Resolved in next
1.1that come back to simpler modules & binding like in your case.