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?
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. But just with the convention, you make separate Dao for every entity. This way your code will be organized, that's all.
When you use the Room persistence library to store your app's data, you interact with the stored data by defining data access objects, or DAOs. Each DAO includes methods that offer abstract access to your app's database. At compile time, Room automatically generates implementations of the DAOs that you define.
Repository: Repository is a class that is mainly used to manage multiple sources of data. Entity: Entity is an annotated class that is used to describe a database table when we are working with Room.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With