0

我尝试通过以下方式绑定依赖项

fun getKodeinConfigurationsGraph(context: Context, url: String) = Kodein.lazy {
    import(dataBaseModule(context))

    import(retrofitModule(url))
    bind<ConfigurationsService>() with provider { instance<RetrofitHandler>().buildCall(ConfigurationsService::class) }

    import(getConfigurationRepository<Contact>(ConfigurationDataType.CONTACT))

}

fun <T> getConfigurationRepository(type: ConfigurationDataType) =
        Kodein.Module {
            bind<BaseDao<Contact>>() with provider { instance<AppDatabase>().getContactDao() }
            bind<LocalDataSource<T>>() with provider { LocalDataSourceImpl<T>(dao = instance()) }
            bind<BaseRestHandler<List<T>>>() with provider { ConfigurationsHandler<T>(configurationsService = instance(), type = type) }
            bind<RemoteDataSource<List<T>>>() with provider { RemoteDataSourceImpl<List<T>>(restHandler = instance()) }
            bind<Repository<T>>() with provider { BasicRepository<T>(localDataSource = instance(), remoteDataSource = instance()) }
        }

但我收到以下错误

java.lang.IllegalArgumentException: bind<LocalDataSource<T>>() uses a type variable named T, therefore, the bound value can never be retrieved.

我想导入模块并获得所需的依赖项,而无需每次都重复相同的绑定。

有没有办法做到这一点?

4

1 回答 1

0

尝试更改为inline fun <reified T> getConfigurationRepository(...). 然后它会像你bind每次都重复 s 一样工作。有关文档,请参阅https://kotlinlang.org/docs/reference/inline-functions.html

bind本身也使用了具体的类型参数。

于 2018-03-28T06:47:17.180 回答