InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
8.77k stars 695 forks source link

The Constructor DSL could inject parameters via getOrNull() #1812

Open gary0707 opened 3 months ago

gary0707 commented 3 months ago

Is your feature request related to a problem? Please describe. In my code base, some of constructors use optional (nullable) parameters, which are not resolved correctly when I use the Constructor DSL like:

class FizzClass(val initialValue: Int?)

val someModule = module {
    factoryOf(::FizzClass)
}

The FizzClass is sometimes inject with a non-null value, but sometimes with null and to make that work I need to go with the classic DSL like:

val someModule = module {
    factory {
        FizzClass(initialValue = getOrNull())
    }
}

Describe the solution you'd like It would be great to be able to use the Constructor DSL also with nullable injected params.

Describe alternatives you've considered The only alternative I can see for now is the go with much more verbose classic Koin DSL.

Target Koin project Koin-Core

Nek-12 commented 2 months ago

Duplicate of #1456