android10 / Android-CleanArchitecture

This is a sample app that is part of a series of blog posts I have written about how to architect an android application using Uncle Bob's clean architecture approach.
Apache License 2.0
15.52k stars 3.32k forks source link

UseCase for each method of repository? #227

Open mHerasimov opened 7 years ago

mHerasimov commented 7 years ago

Does we need to create UseCases for each method from Repository interface in domain layer?

For example assume that I have such Repository interface

interface ThingRepository {
    void create(Thing thing);
    void delete(Thing thing);
    List<Thing> readAll();
    int size();
}

As you can see there is size() method that returns number of records in database or in file, whatever. And this method is pretty fast. I guess that there is no need for UseCase for this method because it wouldn't block UI thread and can be executed synchronously.

So could you explain me when you create UseCases and when you don't. Basically is there any rules for UseCase creation?

Sorry if there is some misunderstanding in this issue.

PhungVanHoa commented 7 years ago

why no one answer ?. i am also interested in this

mHerasimov commented 7 years ago

ok, let's summarize, I see 2 versions

  1. use Interactor & Presenter & View for simple action
  2. perform call from presentation layer to domain layer directly

use Interactor & Presenter & View pros

cons

perform call from presentation layer to domain layer directly pros

cons

currently I think of creation of interface like this for synchronos simple actions

interface SyncUseCase<T, R> {
    R perform(T t);
}

it will remove direct calls from Activity/Fragment to Repositopy

currently I am not sure which way is right and it'll be good if someone correct me

mHerasimov commented 7 years ago

@chanhoncongian I'll ask on SO, meaby somebody will answer here is question

mHerasimov commented 7 years ago

Hi guys, eventually I came across this issue. I've read code of various repos related to Clean Architecture including this one. Author of this repo creates interactor for each repository method. But we can use one presenter for multiple interactors, as author does. Just take a look at interactors and presenters implementations. So we should design our presenters, keeping in mind views (Fragment/Activity) for which we are designing them.

swapii commented 6 years ago

As you can see there is size() method that returns number of records in database or in file, whatever. And this method is pretty fast.

I think we (developers) shouldn't think like this. We don't know how long each repository method would be executing. And there is the answer. Each repository method must be executed asynchronously. We can write UseCase for this or invoke repository method directly but without blocking main thread.