gpt4 book ai didi

android - 在单例私有(private)构造函数类上使用 hilt 进行依赖注入(inject)

转载 作者:行者123 更新时间:2023-12-04 23:47:26 26 4
gpt4 key购买 nike

我是新手。所以我想在我的使用 MVVM 架构的项目上尝试依赖注入(inject)。
结构如下所示:JsonHelper -> RemoteDataSource -> Repository -> ViewModel。
当我尝试在 RemoteDataSource 和 Repository 上注入(inject)我的 DI 时会出现问题,因为这些类是单例类并且具有私有(private)构造函数。
错误代码如下所示

..location\RemoteDataSource.java:40: error: Dagger does not support injection into Kotlin objects
public static final class Companion {

..location\Repository.java:30: error: Dagger does not support injection into Kotlin objects
public static final class Companion {
这些是我的 RemoteDataSource 和 Repository 代码,我尝试将它注入(inject)构造函数,但它说 Dagger 不能注入(inject)私有(private)构造函数,所以我尝试将它注入(inject)函数但仍然没有工作
远程数据源.kt
@Singleton
class RemoteDataSource private constructor(private val jsonHelper: JsonHelper) {

companion object {

@Volatile
private var instance: RemoteDataSource? = null

@Inject
fun getInstance(jsonHelper: JsonHelper): RemoteDataSource =
instance ?: synchronized(this) {
instance ?: RemoteDataSource(jsonHelper).apply { instance = this }
}

}

fun getAllRemoteMovies(moviesCallback: LoadMoviesCallback) {
moviesCallback.onAllMoviesReceived(jsonHelper.loadRemoteMovies())
}

fun getAllRemoteTVShows(tvshowCallback: LoadTVShowCallback) {
tvshowCallback.onAllTVShowsReceived(jsonHelper.loadRemoteTVShows())
}

interface LoadMoviesCallback {
fun onAllMoviesReceived(moviesResponses: ArrayList<MovieItem>)
}

interface LoadTVShowCallback {
fun onAllTVShowsReceived(tvshowResponses: ArrayList<TVShowItem>)
}

}
存储库.kt
@Singleton
class Repository private constructor(private val remoteDataSource: RemoteDataSource) : DataSource {

companion object {

@Volatile
private var instance: Repository? = null

@Inject
fun getInstance(remoteDataSource: RemoteDataSource): Repository =
instance ?: synchronized(this) {
instance ?: Repository(remoteDataSource).apply { instance = this }
}

}

override fun getAllRemoteMovies(): LiveData<ArrayList<MovieItem>> {
val remoteMoviesResult = MutableLiveData<ArrayList<MovieItem>>()
remoteDataSource.getAllRemoteMovies(object : RemoteDataSource.LoadMoviesCallback {
override fun onAllMoviesReceived(moviesResponses: ArrayList<MovieItem>) {
remoteMoviesResult.value = moviesResponses
}
})
return remoteMoviesResult
}

override fun getAllRemoteTVShows(): LiveData<ArrayList<TVShowItem>> {
val remoteTVShowsResult = MutableLiveData<ArrayList<TVShowItem>>()
remoteDataSource.getAllRemoteTVShows(object : RemoteDataSource.LoadTVShowCallback {
override fun onAllTVShowsReceived(tvshowResponses: ArrayList<TVShowItem>) {
remoteTVShowsResult.value = tvshowResponses
}
})
return remoteTVShowsResult
}

}
这是我的注入(inject)模块
远程数据源模块.kt
@Module
@InstallIn(ActivityComponent::class)
object RemoteDataSourceModule {

@Singleton
@Provides
fun provideJsonHelper(context: Context): JsonHelper {
return JsonHelper(context)
}

@Singleton
@Provides
fun provideRemoteDataSource(jsonHelper: JsonHelper): RemoteDataSource {
return RemoteDataSource.getInstance(jsonHelper)
}

@Singleton
@Provides
fun provideRepository(remoteDataSource: RemoteDataSource): Repository {
return Repository.getInstance(remoteDataSource)
}

}
那么如何在不将类构造函数更改为 public 的情况下解决这个问题呢?

最佳答案

@Singleton 注释足以通知该类是单例类,所以我只需删除伴随对象并使用公共(public)构造函数更改私有(private)构造函数,因此代码将如下所示:

@Singleton
class RemoteDataSource @Inject constructor(private val jsonHelper: JsonHelper) {
// Your codes
}

关于android - 在单例私有(private)构造函数类上使用 hilt 进行依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67453418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com