在我的应用程序中,我有两个模块:app和repository.
repository依赖于 Room,并且有一个GoalRepository接口:
interface GoalRepository
和一个GoalRepositoryImpl内部的类,因为我不想将它或 Room 依赖暴露给其他模块:
@Singleton
internal class GoalRepositoryImpl @Inject constructor(private val dao: GoalDao) : GoalRepository
app取决于repository获取GoalRepository实例。
目前,我有一个GoalRepositoryModule是:
@Module
class GoalRepositoryModule {
@Provides
@Singleton
fun provideRepository(impl: GoalRepositoryImpl): GoalRepository = impl
@Provides
@Singleton
internal fun provideGoalDao(appDatabase: AppDatabase): GoalDao = appDatabase.goalDao()
@Provides
@Singleton
internal fun provideDatabase(context: Context): AppDatabase =
Room.databaseBuilder(context, AppDatabase::class.java, "inprogress-db").build()
}
问题是这不会编译(显然),因为公共provideRepository函数正在公开GoalRepositoryImpl,即一个internal类。
如何构建我的 Dagger 设置以实现我想要的?
编辑:
我尝试provideRepository按照@David Medenjak 评论进行内部设置,现在 Kotlin 编译器抱怨它无法解决 RoomDatabase 依赖项:
Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
class xxx.repository.database.AppDatabase, unresolved supertypes: androidx.room.RoomDatabase
为了完整起见,我的组件在app模块内的代码:
@Component(modules = [ContextModule::class, GoalRepositoryModule::class])
@Singleton
interface SingletonComponent