koin icon indicating copy to clipboard operation
koin copied to clipboard

Passing Parameters not work - NoParameterFoundException

Open LeaveX007 opened this issue 3 years ago • 1 comments

Hy,

i have a class something like that

class MyClassImpl(id:Long) { }

my scope is that scope(named("MyClass")) { scoped<MyClassImpl> { (id: Long) -> BotServiceImpl(id) } }

I create a instance for thes Scope like that: val scope = getKoin().createScope("MyScope", named("MyClass")) val MyClassImpl by scope.inject<MyClassImpl>{parametersOf(100L)}

If the code is starting, then i get an Error ... Can't get injected parameter #0 from DefinitionParameters[] for type 'java.lang.Long'

I don´t know why. Can anyone help me please? Thank you.

Br Mike

LeaveX007 avatar Feb 27 '22 11:02 LeaveX007

I have something similar with koin 3.2.0-beta-1.

This is my actual code:

class AndroidComicBookRepositoryTest : KoinTest {
    private val ctx = InstrumentationRegistry.getInstrumentation().context
    private val repo: AndroidComicBookRepository by inject { parametersOf(ctx) }

    @JvmField
    @RegisterExtension
    val koinTestExtension = KoinTestExtension.create {
        modules(androidKoinModule,
            module {
                single { params ->
                    logging().info{"${params.size()} params:  $params -> ${params.values}"}

                    AndroidDatabaseProvider(ctx = params.get(), dbName = "test")
                }
            })
    }
}

AndroidDatabaseProvider is defined in androidKoinModule but I override it for the test with a different DB name.

params is empty and this is the error I get:

     Caused by: org.koin.core.error.DefinitionParameterException: No value found for type 'android.content.Context'
        at org.granchi.snikt.AndroidComicBookRepositoryTest$koinTestExtension$1$1$1.invoke(AndroidComicBookRepositoryTest.kt:235)
        at org.granchi.snikt.AndroidComicBookRepositoryTest$koinTestExtension$1$1$1.invoke(AndroidComicBookRepositoryTest.kt:34)
        at org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:54)
        	... 148 more

I thought it could be because I'm providing the parameters in the top type I want to inject, and perhaps expecting them to be passed down transitively was too much, so I changed the injection line for:

    private val repo: AndroidComicBookRepository by inject()
    private val dbProvider: AndroidDatabaseProvider by inject { parametersOf(ctx) }

Same result. :(

This is how I have setup Koin in my multiplatform project:

        val commonMain by getting {
            dependencies {
                api("io.insert-koin:koin-core:$koinVersion")
            }
        }

        val androidMain by getting {
            dependencies {
                implementation("io.insert-koin:koin-android:$koinVersion")
            }
        }

Is there anything I'm overlooking? Thanks.

serandel avatar May 06 '22 11:05 serandel

Not reproduced in 3.2.0. I can pass parameter to scoped definition:

        val scopeId = "user_scope"
        val scopeKey = named("KEY")
        val koin = koinApplication {
            modules(
                module {
                    scope(scopeKey) {
                        scoped { (id : Int) -> Simple.MySingle(id) }
                    }
                }
            )
        }.koin

        val scope = koin.createScope(scopeId, scopeKey)
        val id = 42
        val single by scope.inject<Simple.MySingle> { parametersOf(id) }
        assertEquals(id, single.id)
        scope.close()

arnaudgiuliani avatar Aug 29 '22 16:08 arnaudgiuliani