8

I was wondering what is the exact application of UseCase in Clean Architecture. Since, nowadays when you have a remote data source or rest API, the heavy processes are done on the server-side so you don't have any logic to implement in UseCase. All logic is state management, to handle loading errors, etc. and these have to be inside state management. Am I wrong? Do you have a scenario where UseCases are needed?

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
reza
  • 1,354
  • 1
  • 7
  • 25

2 Answers2

12

BLOCs control the state of widgets. Ideally, they don't have business logic. They just use UseCases.

UseCases (or Interactors) are a business logic of application. They use entities and abstract repositories. They're needed when you want to use the same business logic in several blocs. Instead of copy paste the piece of code from the existed bloc you just make 1 UseCase and use it in all the blocs you want.

  1. UseCase uses abstract repositories. It means that UseCase doesn't depend from data source. It could be Firestore, REST api, local database etc. UseCase just know that repository returns Entities. Business logic becomes independent from data source and easy to test.
  2. API calls are made in repository implementations. For example, we have abstract class "Repository" and it's implementations: "FirebaseRepository", "ApiRepository", "LocalRepository" etc. Thanks by polymorpthysm we can create UseCases and give them these repository implementations via constructors. It's recommended to use dependency injector like get_it.
  3. Bloc doesn't contain any business logic. In Clean Architecture terms bloc = controller + presenter. Bloc just uses UseCases and business logic can be reused between several blocs.
Mr Mister
  • 59
  • 6
  • 1. "UseCase doesn't depend from data source". `Domain` layer depends only on `Entity` layer, but using `InverseDependencyRule` it can access `DataSource` interface. Repository are optional. The most important in CA is to stick to "TheDependencyRule". 2. API calls are made in `DataSource`. Repository behaves like a proxy which decide which `DataSources` should be used and encapsulate this logic in `data` layer. Respository are also used to cache data. – murt Nov 29 '22 at 11:37
2

Imho even though there is hardly any logic to implement in UseCase, for example just passing params. Creating useCase will allow us freely change the repository based on our needs. For example, different apps use different logic in the repository.

Clean Architecture benefit is not only about layering logic, but how to increase reusability, testability, and scalability (easier to change and modify in the future)

Faruk
  • 5,438
  • 3
  • 30
  • 46