How to inject dependency using koin in top level function

I did it in this way

fun Route.general() {

val repo: OperationRepo by lazy { GlobalContext.get().koin.get() }
...
}

Easiest way is to create KoinComponent object as wrapper and then to get variable from it:

val imageManager = object:KoinComponent {val im: ImageManager by inject()}.im

Btw its better to wrap it by some function, for example I use

inline fun <reified T> getKoinInstance(): T {
    return object : KoinComponent {
        val value: T by inject()
    }.value
}

So if I need instance I just write

val imageManager:ImageManager = getKoinInstance()

or

val imageManager = getKoinInstance<ImageManager>()

Tags:

Kotlin

Koin