Reputation: 9925
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?
Upvotes: 18
Views: 10591
Reputation: 11
This is how I did it in Kotlin
First reset your database by clearing/deleting application data in avd or physical device depending on where you have installed your app
Alternatively, you can create a new room database
Create your Entity classes
First entity class
@Entity(tableName = "news_table_name")
data class ArticleObject(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "row_id")
var rowId: Int,
@ColumnInfo(name = "article_id")
var articleId: String
)
Second entity class
@Entity(tableName = "users_table")
data class UserObject (
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "rowId")
val rowId:Int,
@ColumnInfo(name = "author_user_id")
val authorUserId:Int,
@ColumnInfo(name = "author_user_name")
val authorUserName:String,
)
Dao interface
@Dao
interface NewsDao {
@Insert
suspend fun insertUser(userObject: UserObject)
@Insert
suspend fun insertNews(articleObject: ArticleObject)
}
Room database
@Database(entities = [ArticleObject::class, UserObject::class], version = 1, exportSchema = false)
abstract class NewsDatabase : RoomDatabase() {
abstract fun newsDao(): NewsDao
companion object {
@Volatile
private var INSTANCE: NewsDatabase? = null
fun getInstance(context: Context): NewsDatabase? {
synchronized(this) {
var instance = INSTANCE
if (instance == null) {
instance = Room.databaseBuilder(
context.applicationContext,
NewsDatabase::class.java,
"news_database"
).build()
}
return instance
}
}
}
}
Therefore, yes you can create and work with multiple tables in a single database, just remember to create separate entity classes
Upvotes: 0
Reputation: 6882
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.
Upvotes: 32