如何使用储存库

时间:2019-06-09 14:28:46

标签: android repository mvp presenter

我将Dagger2和Moxy用于MVP。据我了解,Presenter可以调用存储库以从数据库加载和卸载数据。但是我只是不知道如何创建存储库的实例:在Dagger的帮助下在Activity中并转移到演示者还是演示者本身?

我在Activity中使用存储库,但我认为这是一种反模式。

它提供上下文

 @Module
 public class AppModule {
   private Context context;

   public AppModule(Context context){
       this.context = context;
   }

   @Singleton
   @Provides
   Context provideContext(){
       return context;
   }
 }

此模块提供会议室

 @Module
 public class RoomModule {
   @Singleton
   @Provides
   AppDataBase providesAppDataBase(Context context) {
     return Room.databaseBuilder(context, AppDataBase.class, "budget")
             .fallbackToDestructiveMigration()
             .allowMainThreadQueries()
             .build();
   }

@Singleton
@Provides
BudgetDao providesDao(AppDataBase database) {
    return database.getBudgetDao();
 }

 @Singleton
 @Provides
 DetailDao providesDetailDao(AppDataBase dataBase){
     return dataBase.getDetailDao();
 }
  }

AppComponent

@Singleton
@Component(modules = {RoomModule.class, AppModule.class})
public interface AppComponent {

void inject(BudgetListPresenter presenter);

void inject(BudgetsActivity activity);

void inject(DetailActivity activity);
 }

Repository.class

 @Singleton
 public class BudgetListRepository implements BudgetRepository {
 private BudgetDao budgetDao;

 @Inject
 public BudgetListRepository(BudgetDao budgetDao){
     this.budgetDao = budgetDao;
 }

 @Override
 public void updateBudget(Budget budget) {
     budgetDao.updateBudget(budget);
 }

 @Override
 public void addBudget(Budget budget) {
     budgetDao.insertBudget(budget);
 }

 @Override
 public void deleteBudget(Budget budget) {
     budgetDao.deleteBudget(budget);
 }

 @Override
 public Budget getBudget(String id) {
     return budgetDao.getBudget(id);
 }

 @Override
 public List<Budget> getAll() {
     return budgetDao.getAll();
 }
}

1 个答案:

答案 0 :(得分:0)

流程应该是这样

首先,您从视图中调用一个方法给演示者。

然后,在演示者中,您需要与域层中的任何用例进行通信,并且该层有责任使用匕首注入存储库。

让我给你看一个例子

从演示者那里注入您的用例(交互器),这将是负责人以后与存储库进行通信

class LoginPresenter @Inject constructor(private val signInInteractor: SignInInteractor) : LoginContract.Presenter {
...

override fun signInWithEmailAndPassword(email: String, password: String) {
signInInteractor.signInWithEmailAndPassword(email, password)
...

然后您的交互者应该像这样注入存储库

class SignInInteractorImpl @Inject constructor(val userRepository: UserRepository): SignInInteractor{
...

 override suspend fun pushUserIntoDatabase(account: GoogleSignInAccount): Unit = suspendCancellableCoroutine { continuation ->
        userRepository.createUser(account.displayName!!,account.email!!,object : UserRepository.UserRepositoryCallback{

            override fun onRemoteSuccess() {
                //success
            }

            override fun onRemoteFailure(errormsg:String) {
                //failure
            }

        })

    }

在这种情况下,我只在用例中使用对存储库的调用以将数据推送到Firebase

然后在我的 PresentationModule 中,我注入了该仓库

 @Provides
    @Singleton
    fun provideUserRepositoryToLogin(userRepository: UserRepository): SignInInteractor {
        return SignInInteractorImpl(userRepository)
    }

这是要遵循的概念

enter image description here