'Single or multiple DAO and Repository in Android Room database project?

I have a Room database project which has a DAO and a Repository (mediator between different data sources) for each table in the database. It is a lot files and class names to remember.

I would like to know if there a disadvantages for using a single Repository and DAO class per project?



Solution 1:[1]

There is no such rule that you have to make separate @Dao for every table. You can make one Dao class that holds all of your database queries.

@Dao
interface MyDao{
    @Query("SELECT * FROM Student")
    fun getStudents(): List<User>

    @Query("SELECT * FROM Professors")
    fun getProfs(): List<User>
}

But just with the convention, you make separate Dao for every entity. This way your code will be organized, that's all. It's difficult to review your code if you have bunch of unrelated queries within the same Dao.

For repositories, I would have different approach. Although there is no such rule that you have to use separate Repository or single Repository, I would use both wherever I think the one is necessary.

For example, if I have only one database call from User table and one from Post table, I don't see point of having two separate repositories for each table. So I would just make single repository in a such case. But on the other hand, if I have 10 database calls from User table and 10 more from Post table, I would split repositories and have two repositories one for each table.

All in all, these are just conventions and above are just suggested style of coding.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1