koin icon indicating copy to clipboard operation
koin copied to clipboard

Linked scopes order should be reversed / controllable

Open tlevavasseur-decathlon opened this issue 3 years ago • 1 comments

Describe the bug You can override a global single using a scope but you cannot do that with another scope linked to the first one doing this because of the order of scopes (root first).

ie : a global single bean, an override in the activity scope and used via the fragment scope.

To Reproduce

class ScopeTests {

    companion object {

        val koin = startKoin {
            modules(
                module {
                    single { MyBean("SINGLETON") }
                    scope(named("direct")) { scoped { MyBean("SCOPED") } }
                    scope(named("indirect")) { /* we will link this scope to "direct" */ }
                }
            )
        }.koin
    }

    class MyBean(val name: String)

    @Test
    fun testSingleton() {
        val bean = koin.get<MyBean>()
        assertEquals("SINGLETON", bean.name)
    }

    @Test
    fun testScopeDirectOverride() {
        val scope = koin.getOrCreateScope("direct", named("direct"))
        val bean = scope.get<MyBean>()
        assertEquals("SCOPED", bean.name)
    }

    @Test
    fun testScopeIndirectOverride() {
        val direct = koin.getOrCreateScope("direct", named("direct"))
        val indirect = koin.getOrCreateScope("indirect", named("indirect"))
        indirect.linkTo(direct)
        val bean = indirect.get<MyBean>()
        assertEquals("SCOPED", bean.name) // fails because gets SINGLETON
    }
}

Expected behavior At least root scope should be checked last but I think the checking order in Scope.findInOtherScopes should be reversed.

Koin project used and used version (please complete the following information): implementation "io.insert-koin:koin-android:3.2.0"

tlevavasseur-decathlon avatar Aug 19 '22 17:08 tlevavasseur-decathlon

Parking your proposal in 4.0.0 for further new scope improvements 👍

arnaudgiuliani avatar Aug 29 '22 13:08 arnaudgiuliani

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 26 '23 13:01 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 25 '23 13:06 stale[bot]

Hi, @arnaudgiuliani any updates on this? will this be solved or will it remain in wont-fix status?

sarthakz9 avatar Dec 01 '23 04:12 sarthakz9