I've recently started applying the Clean Architecture while developing an Android application. And so I divided my application in 4 different parts.
Data layer
Contains the implementation of repositories.
Device layer
Contains implementation of android related stuff like AlarmManager, not related to storage of entities.
Domain layer
Use cases (interactors) are implemented here and interfaces for repositories are defined here.
Presentation
UI related stuff, fragments, view models, activities.
This application is not in isolation, it exists in a system with a backend and a frontend (web app) where some users use the mobile app and some use the web app.
Now, according to the clean architecture, the business logic should reside in the domain layer. But in this scenario I feel like it does not belong there. Let's take an example.
Let's say a User can purchase MovieTickets from different type of cinemas.
In this scenario, we would have a use case called PurchaseMovieTicket.
Now the question is. Where should the actual logic on the purchase be implemented? There are two ways this can be done.
Type 1
Most examples on clean architecture on android only consider the Repository as something to do CRUD operations on whether its on the cloud or local storage (eg. Android Room). This would mean the PurchaseMovieTicket use case would include logic such as.
- Remove money from user
- Add the movie ticket to the user
- Call update on the repository with the User as parameter
Now the drawbacks here is obvious. What if the backend needs to do other things related to purchasing of movie tickets, perhaps communicate to other systems even, then all this would also need to be implemented in the app. You can almost see how this is not possible.
Type 2
The Repository contains a method called purchaseMovieTicket which takes a MovieTicket and a User. This method would do the necessary logic on the backend or wherever it is implemented.
Drawback here is well, the use case becomes almost unnecessary because it just forwards the call from the view model to the repository. And there is no business logic to talk about.
Question
- Which type would be the best to use in this system? Why?
- Is it good to put the business logic in the backend?
- If you go with implementation type 2, does the repository break the clean architecture principles?