I've got a Room database for my Android application. My FooDao and BarDao both implement BaseDao:
interface BaseDao<T> {
@Insert
fun insert(obj: T): Long
@Update
fun update(obj: T)
}
@Dao
interface FooDao: BaseDao<FooEntity> {
@Query("some query")
fun doSomethingWithFoo()
}
@Dao
interface BarDao: BaseDao<BaseEntity> {
@Query("some other query")
fun doSomethingWithBar()
}
I have a repository class as well:
class MyRepo(private val fooDao: FooDao, private val barDao: BarDao) {
fun doSomethingWithFoo() = fooDao.doSomethingWithFoo()
fun doSomethingWithBar() = barDao.doSomethingWithBar()
}
But imagine that FooDao, BarDao, and MyRepo have many more than just these two functions.
I have learned about class delegation to enable compositional inheritance. I am thus trying to reduce boilerplate in MyRepo by having it delegate simple functions that just call a dao's function and return the dao's return value. It makes sense that I should do:
class MyRepo(...): FooDao by fooDao, BarDao by barDao {
//fun doSomethingWithFoo no longer necessary
//fun doSomethingWithBar no longer necessary
}
However, Android Studio gives me the red squiggly saying Type parameter T of BaseDao has inconsistent values: FooEntity, BarEntity
Does this mean all hope is lost for reducing all this boilerplate like MyRepo.doSomethingWithFoo = fooDao.doSomethignWithFoo? Or is there a way to accomplish this?
I tried to actually override the BaseDao functions in MyRepo thinking it would stop complaining about FooEntity and BarEntity:
class MyRepo(...): BaseDao<Any>, SetDao by setDao, ... {
override fun insert(t: Any): Long = 0L
}
but that just adds "Any" to the inconsistent types message.