MVVM - Having a hard time understanding how to create the Domain layer in Clean Architecture

Viewed 1002

I'm trying to learn MVVM to make my app's architecture more clean. But I'm having a hard time grasping how to create a "Domain" layer for my app.

Currently this is how the structure of my project is looking:

My View is the activity. My ViewModel has a public method that the activity can call. Once the method in the ViewModel is called, it calls a method in my Repository class which performs a network call, which then returns the data back to the ViewModel. I then update the LiveData in the ViewModel so the Activity's UI is updated.

This is where I'm confused on how to add a Domain layer to the structure. I've read a lot of Stackoverflow answers and blogs about the Domain layer and they mostly all tell you to remove all the business logic from the ViewModel and make a pure Java/Kotlin class.

So instead of

View --> ViewModel --> Repository

I would be communicating from the ViewModel to the Domain class and the Domain class would communicate with the Repository?

View --> ViewModel --> Domain --> Repository

I'm using RxJava to make the call from my ViewModel to the Repository class.

@HiltViewModel
public class PostViewModel extends ViewModel {

    private static final String TAG = "PostViewModel";

    private final List<Post>                  listPosts              = new ArrayList<>();
    private final MutableLiveData<List<Post>> getPostsLiveData       = new MutableLiveData<>();
    private final MutableLiveData<Boolean>    centerProgressLiveData = new MutableLiveData<>();
    private final MainRepository              repository;

    @Inject
    public PostViewModel(MainRepository repository) {
        this.repository = repository;
        getSubredditPosts();
    }

    public void getSubredditPosts() {
        repository.getSubredditPosts()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        centerProgressLiveData.setValue(true);
                    }

                    @Override
                    public void onNext(@NonNull Response response) {
                        Log.d(TAG, "onNext: Query called");
                        centerProgressLiveData.setValue(false);
                        listPosts.clear();
                        listPosts.addAll(response.getData().getChildren());
                        getPostsLiveData.setValue(listPosts);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e(TAG, "onError: getPosts", e);
                        centerProgressLiveData.setValue(false);
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }
public class MainRepository {

    private final MainService service;

    @Inject
    public MainRepository(MainService service) {
        this.service = service;
    }

    public Observable<Response> getSubredditPosts() {
        return service.getSubredditPosts();
    }
}

Could someone please give me an example of how I could do it? I'm quite lost here

2 Answers

I had a hard time while trying to figure out the domain layer. The most common example of it is the use case.

Your viewmodel won't communicate directly to the repository. As you said, you need viewmodel 》domain 》repository.

You may think of a usecase as a abstraction for every repository method.

Let's say you have a Movies Repository where you call a method for a movie list, another method for movie details and a third method for related movies.

You'll have a usecase for every single method.

What's the purpose of it?

Let's say you have a DetailActivity that communicate with a Detail Viewmodel. Your viewmodel doesn't need to know all the repository (what's the purpose of calling a movie list method on you Detail screen?). So, all your DetailViewModel will know is "Detail Usecase " (that calls the Detail method in repository).

Google has updated the architecture documentation few hours ago, take a look! https://android-developers.googleblog.com/2021/12/rebuilding-our-guide-to-app-architecture.html?m=1&s=09

PS: Usecase is not a special android class, you do not need to inherent any behavior (as fragment, activity, viewmodel...) it's a normal class that will receive the repository as parameter.

You'll have something like:

Viewmodel:

function createPost(post Post){
   createUseCase.create(post)
}

UseCase

function createPost(post Post): Response {
   return repository.create(post)
}

I spent quite a bit of time trying to learn how to add a domain layer using RxJava by reading a lot of blogs and Stackoverflow answers, but all of them were missing the conversion of the response from the api call to what you'd like to display on screen (For example if the back end returns a username dave123 and you'd like to display by dave123).

I finally figured it out and the secret sauce was to use a RxJava .map() operator inside the UseCase class. I also decided to keep the RxJava call inside my ViewModel.

So in my Repository class I have a method that calls the Api and returns a type of Single<Response>. This is the raw json data the Api returns.

public class MainRepository {

    private final MainService service;
    private final PostDao postDao;

    @Inject
    public MainRepository(MainService service, PostDao postDao) {
        this.service = service;
        this.postDao = postDao;
    }

    public Single<Response> getResponse() {
        return service.getSubredditPosts();
    }
}

Inside my GetPostsUseCase class, I'm call the getResponse() method from the MainRepository and altering the Response by performing business logic on it (the stuff I want to display on the UI. In this case I add the String "by " to the username)

And the secret or the part I had alot of trouble understanding/figuring out how to do was converting the Type inside the Single<>. I used the .map() operator to change the return type and filter the Response to a List<Post>

public class GetPostsUseCase {

    private final MainRepository mainRepository;

    @Inject
    public GetPostsUseCase(MainRepository mainRepository) {
        this.mainRepository = mainRepository;
    }

    public Single<List<Post>> getSubredditPosts(){
        return mainRepository.getResponse().map(response ->
                getPostsFromResponse(response.getData().getChildren())
        );
    }

    private List<Post> getPostsFromResponse(List<Child> listChildren) {
        List<Post> listPosts = new ArrayList<>();
        for (Child child : listChildren) {
            Post post = child.getPost();
            post.setCreatedBy("by " + post.getUsername());
            listPosts.add(post);
        }
        return listPosts;
    }
}

And this is how my ViewModel looks like

public class PostViewModel extends ViewModel {

    private static final String TAG = "PostViewModel";

    private final List<Post>                  listPosts              = new ArrayList<>();
    private final MutableLiveData<List<Post>> getPostsLiveData       = new MutableLiveData<>();
    private final MutableLiveData<Boolean>    centerProgressLiveData = new MutableLiveData<>();
    private final GetPostsUseCase             getPostsUseCase;

    @Inject
    public PostViewModel(GetPostsUseCase getPostsUseCase) {
        this.getPostsUseCase = getPostsUseCase;
        getSubredditPosts();
    }

    public void getSubredditPosts() {
        getPostsUseCase.getSubredditPosts()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SingleObserver<List<Post>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        centerProgressLiveData.setValue(true);
                    }

                    @Override
                    public void onSuccess(@NonNull List<Post> list) {
                        Log.d(TAG, "onNext: Query called");
                        centerProgressLiveData.setValue(false);
                        listPosts.clear();
                        listPosts.addAll(list);
                        getPostsLiveData.setValue(listPosts);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        centerProgressLiveData.setValue(false);
                    }
                });
    }

I couldn't find any blogposts or answers that had an example like this. Hopefully this helps anyone out there who is struggling to learn how to implement clean architecture with MVVM, Hilt, RXJava and a Domain layer.

If I did do something incorrectly or not considered clean architecture please let me know.

Related