InsertKoinIO / koin-annotations

Koin Annotations - About Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform insert-koin.io
https://insert-koin.io
Apache License 2.0
159 stars 42 forks source link

`getAll` with function-based singletons only returns a single instance #165

Open SIMULATAN opened 1 month ago

SIMULATAN commented 1 month ago

Describe the bug Using Koin#getAll() to get @Single annotated functions sharing a return type only returns a single instance. The others get lost during registration and don't even show up in the internal Koin _instances map.

To Reproduce

  1. Create a service interface
  2. Implement it anonymously in an arbitrary (2+) number of function bodies
  3. Annotate those functions with @Single
  4. Attempt to use getAll - only one function is returned

Please find a dead simple reproducer here: https://github.com/SIMULATAN/koin-getall-reproducer

Expected behavior All instances are returned.

Koin project used and used version koin-core 4.0.0-RC1 koin-annotations 1.4.0-RC4

Analysis From my analysis, the problem is that koin-annotations registers the singletons using the same binds class: the function return type. This poses an issue with koin's internal instanceRegistry._instances map, which stores the instances using the class name as the key. As established, this class name is the interface type, which causes an arbitrary number of singletons with that type to only result in a single getable instance. reproducer in intellij with the debugger showing the internal koin map In short: using subclasses, getAll works as koin saves the implementations by their concrete class name in the internal map. However, with @Single fun, the "concrete class" becomes the superclass, resulting in overrides in the internal map.

Proposed solution Naming beans created from functions, for example based on the function name. Although not perfect, this may prevent collisions and it serves a similar system as class implementations.

arnaudgiuliani commented 1 month ago

Interesting. Can you just share the generated DSL? A definition should allow several type binding. Annotations should take original class and second type binding

SIMULATAN commented 1 month ago
@Definition("me.simulatan.koin.getall")
public fun Module.defineFirstResource() : KoinDefinition<*> = single() { me.simulatan.koin.getall.FirstResource() } bind(me.simulatan.koin.getall.Resource::class)
@Definition("me.simulatan.koin.getall")
public fun Module.defineSecondResource() : KoinDefinition<*> = single() { me.simulatan.koin.getall.SecondResource() } bind(me.simulatan.koin.getall.Resource::class)
@Definition("me.simulatan.koin.getall")
public fun Module.definefirstResource() : KoinDefinition<*> = single() { me.simulatan.koin.getall.firstResource() } bind(me.simulatan.koin.getall.Resource::class)
@Definition("me.simulatan.koin.getall")
public fun Module.definesecondResource() : KoinDefinition<*> = single() { me.simulatan.koin.getall.secondResource() } bind(me.simulatan.koin.getall.Resource::class)
@Definition("me.simulatan.koin.getall")
public fun Module.definethirdResource() : KoinDefinition<*> = single() { me.simulatan.koin.getall.thirdResource() } bind(me.simulatan.koin.getall.Resource::class)

(I removed the boilerplate)