In my application, I have two modules: app
and repository
.
repository
depends on Room, and has a GoalRepository
interface:
interface GoalRepository
and a GoalRepositoryImpl
class that is internal, as I don't want to expose it or the Room dependency to other modules:
@Singleton
internal class GoalRepositoryImpl @Inject constructor(private val dao: GoalDao) : GoalRepository
app
depends on repository
to get a GoalRepository
instance.
I have a GoalRepositoryModule
that, at the moment, is:
@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()
}
The issue is that this won't compile (obviously) as the public provideRepository
function is exposing GoalRepositoryImpl
, that is an internal
class.
How can I structure my Dagger setup to achieve what I want?
Edit:
I tried making provideRepository
internal as per @David Medenjak comment and now the Kotlin compiler complains that it cannot resolve RoomDatabase dependency:
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
For completeness, the code of my Component inside the app
module:
@Component(modules = [ContextModule::class, GoalRepositoryModule::class])
@Singleton
interface SingletonComponent