InsertKoinIO / koin

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

DependencyResolutionException #117

Closed elsennov closed 6 years ago

elsennov commented 6 years ago

Hi, I got this error.

org.koin.error.DependencyResolutionException: Cyclic call while resolving Bean[class=com.midtrans.travis.login.LoginPresenter]. Definition is already in resolution in current call:
    Bean[class=com.midtrans.travis.login.LoginPresenter]
    Bean[class=com.midtrans.travis.datasource.server.ServerRequestManager]
    Bean[class=com.midtrans.travis.datasource.server.model.ServerApi]
    Bean[name='server_retrofit', class=retrofit2.Retrofit]
    Bean[class=okhttp3.OkHttpClient]

Here are my classes:

class LoginPresenter(private val localRequestManager: LocalRequestManager,
                     private val serverRequestManager: ServerRequestManager) {

class ServerRequestManager(private val localRequestManager: LocalRequestManager,
                           private val serverApi: ServerApi) {

class LocalRequestManager(private val preferenceApi: PreferenceApi) {

And here are my modules:

private val dataSourceModules = applicationContext {
        bean { PreferenceApi(get("application_context")) }
        bean { LocalRequestManager(get()) }
        bean { ServerRequestManager(get(), get()) }
    }

    private val loginModules = applicationContext {
        bean { LoginPresenter(get(), get()) }
    }

Could you help me why am I getting this error?

elsennov commented 6 years ago

Ah, found out the problem. So I need to add explicit casting on the bean declaration. Something like this:

bean("http_logging_interceptor") {
            val httpLoggingInterceptor = HttpLoggingInterceptor()
            httpLoggingInterceptor.level = if (get("log_enabled")) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
            httpLoggingInterceptor as Interceptor
        }
arnaudgiuliani commented 6 years ago

Hello,

yes the DSL is purely descriptive. If your binding against another type (e.g: interface), use the as expression or bind operator:

https://insert-koin.io/docs/1.0/reference/koin-dsl/#binding-additional-types