Hi everyone! Could someone help me? I’m getting in...
# koin
a
Hi everyone! Could someone help me? I’m getting in trouble when trying to declare a module with
ApplicationContext
as parameter… That’s my class:
Copy code
class Foo(environment: ApplicationEnvironment) { ... }
That’s the class which calls
Foo
:
Copy code
class Bar(private val foo: Foo) { ... }
And that’s my
KoinModuleBuilder
:
Copy code
object KoinModuleBuilder {
    fun modules() = listOf(module {
        single {
            Foo(get())
            Bar(get())
        }
    })
}
And finally, that’s the error:
org.koin.error.NoBeanDefFoundException: No compatible definition found for type 'ApplicationEnvironment'
a
if you want to get an instance of
applicationContext
you should use
Copy code
androidContext()
function instead of
get()
call. Also what is
ApplicationEnvironment
means? Is it your application class name? If yes, you can get instance of your application using
androidApplication()
method. But in this case you need to change your parameter class to
Application
and cast in inside your Foo class to correct name. Or make another single that will call
androidApplication() as ApplicationEnvironment
.
a
Oh no, it’s not an Android application. It is for backend.
ApplicationEnvironment
is a Ktor component that allows to get environment variables.
a
I assume there should be something similar for backend
a
I’ll keep searching for. Thank you Vitali! 😉
a
From
koin-ktor
module sources I don’t see any reference that could trigger me something. Might be @arnaud.giuliani could suggest something he is very kind person 😉
a
you need to inject
ApplicationEnvironment
from Ktor?
a
Exactly Arnaud
a
it’s not ready then, we need to add a dsl extension to allow you that like the
ApplicationContext
in android with
androidContext( ... )
DSL extension
either you can define a module that handles this object instance also?
a
@Bruno Ortiz fyi
Folks, just to pin a simple alternative solution (thank you very much @Bruno Ortiz): In Ktor dependencies injection class, we have
installKoin(modulesAsList)
. In that case, as I get Koin modules by a function, I solved that just passing
environment
context to it, while inside Ktor main class, like below:
Copy code
// Ktor module
fun Application.module() {
    installKoin(KoinModuleBuilder.modules(this.environment))
    
    // others installations
    install { ... }
}

// Koin module builder
object KoinModuleBuilder {
    fun modules(applicationEnvironment: ApplicationEnvironment) = listOf(module {
         single { applicationEnvironment }
         ...
         ...
    })
}
Thank you Vitali and Arnaud to every help!
a
good workaround 👍
Would be cool to have this DSL extension also
👍 1