将一个存储库用于多个 Dao 是 MVVM 的最佳实践吗?
Is it best practice MVVM to use one repository for multiple Dao?
我正在尝试使用 MVVM 设计模式构建 Android 应用程序。我目前为 3 种不同类型的 Post 类型创建了 3 种类型的 Dao(Dao1、Dao2、Dao3),使用 Realm 作为数据库。我的 3 个 Daos 看起来很相似,但包含具有一些相同变量的对象,但也有许多不同的变量。
class Dao1(db: Realm) : Dao<PostType1>(db) {
fun findAllAsync(): LiveData<RealmResults< PostType1 >> {
return RealmResultsLiveData(where().findAllAsync())
}
fun findById(id: Int): LiveData< PostType1 >? {
return RealmLiveData(where().equalTo("id", id).findFirst())
}
private fun where(): RealmQuery<CTDCastaway> {
return db.where(PostType1::class.java)
}
}
我想弄清楚我是应该为每个 Dao 创建一个存储库还是为 1 个来统治它们。
class PostRepositary private constructor(val postsDao1: Dao1?){
fun getDao1Posts() = postsDao1?.findAllAsync()
fun getDao1PostById(entityId: Int) = postsDao1?.getById(entityId)
companion object{
@Volatile private var instance:PostRepositary? = null
fun getWildlifeInstance(postsDao1: Dao1) =
instance ?: synchronized(this){
instance ?: PostRepositary(postsDao1).also {
instance = it
}
}
}
}
那么我应该为我的每个 Post 类型创建 1 个存储库,还是为所有 3 个 Post 类型创建 1 个存储库,考虑到它们具有一些共同的属性?
我将如何为多个 Daos 创建一个单一的存储库?
任何指向与此相关的文档的链接将不胜感激。
两个 daos 一个存储库的示例
public class CardRepositoryDummy {
private final CardDao cardDao;
private final UserDao userDao;
private LiveData<List<Card>> cardListLiveData;
private DbHelper db;
private int keystage;
private static final String TAG = "CardRepo";
public CardRepositoryDummy(Application application) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(application);
keystage = sharedPreferences.getInt(Constants.SHARED_PREF_CARD_KEYSTAGE,0);
db = DatabaseBuilder.getDatabase(application);
cardDao = db.cardDaoLive();
userDao = db.userDao();
}
public CardRepositoryDummy(Context application) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(application);
keystage = sharedPreferences.getInt(Constants.SHARED_PREF_CARD_KEYSTAGE,0);
db = DatabaseBuilder.getDatabase(application);
cardDao = db.cardDaoLive();
userDao = db.userDao();
}
我正在尝试使用 MVVM 设计模式构建 Android 应用程序。我目前为 3 种不同类型的 Post 类型创建了 3 种类型的 Dao(Dao1、Dao2、Dao3),使用 Realm 作为数据库。我的 3 个 Daos 看起来很相似,但包含具有一些相同变量的对象,但也有许多不同的变量。
class Dao1(db: Realm) : Dao<PostType1>(db) {
fun findAllAsync(): LiveData<RealmResults< PostType1 >> {
return RealmResultsLiveData(where().findAllAsync())
}
fun findById(id: Int): LiveData< PostType1 >? {
return RealmLiveData(where().equalTo("id", id).findFirst())
}
private fun where(): RealmQuery<CTDCastaway> {
return db.where(PostType1::class.java)
}
}
我想弄清楚我是应该为每个 Dao 创建一个存储库还是为 1 个来统治它们。
class PostRepositary private constructor(val postsDao1: Dao1?){
fun getDao1Posts() = postsDao1?.findAllAsync()
fun getDao1PostById(entityId: Int) = postsDao1?.getById(entityId)
companion object{
@Volatile private var instance:PostRepositary? = null
fun getWildlifeInstance(postsDao1: Dao1) =
instance ?: synchronized(this){
instance ?: PostRepositary(postsDao1).also {
instance = it
}
}
}
}
那么我应该为我的每个 Post 类型创建 1 个存储库,还是为所有 3 个 Post 类型创建 1 个存储库,考虑到它们具有一些共同的属性?
我将如何为多个 Daos 创建一个单一的存储库?
任何指向与此相关的文档的链接将不胜感激。
两个 daos 一个存储库的示例
public class CardRepositoryDummy {
private final CardDao cardDao;
private final UserDao userDao;
private LiveData<List<Card>> cardListLiveData;
private DbHelper db;
private int keystage;
private static final String TAG = "CardRepo";
public CardRepositoryDummy(Application application) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(application);
keystage = sharedPreferences.getInt(Constants.SHARED_PREF_CARD_KEYSTAGE,0);
db = DatabaseBuilder.getDatabase(application);
cardDao = db.cardDaoLive();
userDao = db.userDao();
}
public CardRepositoryDummy(Context application) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(application);
keystage = sharedPreferences.getInt(Constants.SHARED_PREF_CARD_KEYSTAGE,0);
db = DatabaseBuilder.getDatabase(application);
cardDao = db.cardDaoLive();
userDao = db.userDao();
}