Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between repositories and usecases?

I'm creating a chat application using clean architecture, I want to check if the user is logged in when the app starts, and open the login screen if he is not logged in, so my questions are:

  1. What is the best way to implement that? Should I make the LoginActivity the launcher activity and check when the LoginPresenter starts if the user is already logged in then open the MainActivity? And where should I put the logic for checking if the user is authenticated (IsLoggedInUseCase maybe?)?

  2. I don't really understand what is the difference between repositories and usecases, why should I make a GetAllUsersUseCase and EditUserUseCase .. etc, when there is already UsersRepository.getAllUsers() and UsersRepository.editUser(User user)? Why making a whole new class just to reference the the method that already exists in the repository?

like image 540
Mathew Hany Avatar asked Mar 27 '17 19:03

Mathew Hany


1 Answers

Simply, Use-Cases handles your business logic, Repositories are the data layer which you store and access data.

For example when you open the Launcher activity (Let's call it SplashActivity)

First you start your Presenter:

mSplashPresenter.start();

Secondly, in your Presenter's start method you implement a logic if user is logged in or not? if is login navigate to dashboard, if not navigate to LoginActivity.

I assume you have a LoginUseCase.

public void start(){
      if(mLoginUseCase.isLoggedIn()){
          mView.navitageToDashboard();
      } else {
          mView.navigateToLogin();
      }
}

Third, you need a use case method like the following. (Again i assume you have a UserRepository)

public boolean isLoggedIn(){
     // This is your business logic.
     return mUserRepository.getCurrentUser() != null;
}

And in your User Repository:

public User getCurrentUser(){
    // This is your data 
    // You can access remote or local data with repository. 
    return mLocalDataSource.getUser();
}

So why we need a Use-Case? It's a simple business logic which decides if user logged in or not. This can be a more complicated business logic or you want to use this logic in other presenters. So with Use-Cases, you make your business code re-usable and avoid code duplicate in your presenters.

Later if you want to decide to change your login logic you only change your use-case, not all the presenters.

Let's decide a logic for your question: EditUser.

You have a Repository method UsersRepository.editUser(User user) which edit's the user.

You have a Profile screen which user can edit all fields. Also you have a EditScreenDetail screen that user can edit some of the fields which related by screen details can be seen by other people.

In both screen you edit the user but before call UserRepository method you need to check the required fields which is different by two screens. So you define a ProfileEditUseCase and ScreenDetailsEditUseCase to implement two different business logic. But the final operation is same. You edit the user by your repo. From remote or local.

Summary:

With Use-Cases you separate your business logic from presenters and data layer, avoid code duplicate in your presenters. Also you manage your business which can be used in other parts from one class.

I hope i explained it clearly.

like image 177
savepopulation Avatar answered Nov 18 '22 05:11

savepopulation