Back to results for “Dependency Injection”
Flutter › Flutter Architecture Patterns (MVVM & Clean Architecture)

How do use cases, sometimes also called interactors, in Clean Architecture help encapsulate one single specific piece of application business logic, keeping it fully independent of any specific user interface or specific data source implementation?

Intermediate

Answer

A use case represents one single specific, clearly defined piece of application business logic, such as placing an order or calculating an applicable discount, exposing one clear, focused method that a ViewModel or presenter calls, and this use case itself depends only on abstract repository interfaces rather than any concrete, specific implementation, meaning the exact same core use case logic can genuinely be reused across several different specific user interface screens, and more easily and thoroughly tested in complete isolation without needing to also involve or set up any actual concrete networking or database implementation at all.
class PlaceOrderUseCase {
  final OrderRepository repository;
  PlaceOrderUseCase(this.repository);

  Future<Order> execute(Cart cart) => repository.createOrder(cart);
}
Real-world example An e commerce app defines a single PlaceOrderUseCase that is genuinely reused identically by both the main checkout screen and a completely separate quick reorder feature, avoiding duplicating that exact same core order placement business logic in two entirely separate places.

Common follow-ups: How granular should a single use case genuinely be, in terms of how much specific logic it should actually contain?;How do use cases actually interact with a state management solution like Provider or Riverpod?