36

I'm going into Clean Architecture and lift my Android level from MVC to MVP, introducing DI with Dagger 2, Reactivity with RxJava 2, and of course Java 8.

In MVP clean architecture there is a layer between the entities (in datastores) and the presenters that should access them. This layer is the "Use Case". An use case it's ideally an interface, that implements ONE operation on ONE entity.

I also know that Clear Architecture "is screaming", in sense of its projects are really highly readable as the high number of classes in them.

Now, in my project, I have something like 6 different entities, and of course, each entity repository has at least 4 methods (usually get,add,delete,update) to access them.. so, 6 * 4 = 24.

If what I understood until now of Clean Architecture, I will have 24 UseCase.

This is a lot of classes if compared to just 6 controllers in MVC..

Do I really have to make 24 use cases?

I will really appreciate a clarification by someone already used it with success.

4 Answers4

43

Do I really have to make 24 use cases?

Only if everything you write is CRUD.

Refer to the diagram below:

enter image description here

Your assertion is that you will have six different entities, and 4 methods (Create, Read, Update and Delete) for each entity. But that is only true in the yellow circle in the middle of the diagram (the Entities layer). It is pointless to create 24 methods in the Use Cases layer that merely pass through CRUD calls to the Entities layer.

A Use Case is not "Add a Customer Record." A Use Case is more along the lines of "Sell an item to a customer" (which involves Customer, Product, and Inventory entities) or "Print an invoice" (which involves the same entities, in addition to Invoice Header and Invoice Line Items).

When you create Use Cases, you should be thinking about business transactions, not CRUD methods.

Further Reading
Aggregate - a cluster of domain objects that can be treated as a single unit

Robert Harvey
  • 200,592
4

You are right if every CRUD-Operation is translated in one UseCase. But a UseCase may also consist of multiple CRUD-Operations.

A UseCase is a separated model gathers information from different data sources and prepares communication to data sinks. There can be multiple CRUD-Operations be involved.

So think of a UseCase where creating an invoice for a customer AND creating also the customer itself because he/she doesn't exists within the system. You have one UseCase that results in at least two Create-Operations in one transaction.

oopexpert
  • 779
3

It's assumed that clean architecture is used in business heavy applications, where use cases are mapped to business rules. In a CRUD app it's over-engineering. Here you can use facade and map requests directly to the db entity. When logic grows, refactor and extract use cases, validators, domain objects etc..

Glorfindel
  • 3,167
photostok
  • 63
  • 3
1

Your definition of Use Case is wrong, Use Case is a class implementing a business rule, it does not need to be a CRUD operation, it can be a complex multi step operation