D
ArchitectureMedium30 XP3 min read

What is the Use Case layer and when should you use it?

TL;DR: Use cases (interactors) encapsulate a single business operation in the Domain layer. They combine calls to one or more repositories, apply business rules, and return the result. Use them when business logic is non-trivial or must be testable in isolation.

Full Answer

A use case represents a user intention: 'Start a Quiz', 'Mark Question Answered', 'Calculate Streak'. If this operation involves rules (streak resets after 24h, quiz needs min 5 questions), put that logic in a use case, not a BLoC.

When to Skip Use Cases

Simple CRUD with no business rules: GetUserProfile, SaveSettings. These can call the repository directly from the BLoC. Adding a use case layer for trivial pass-throughs is over-engineering.

๐ŸŽฏ

A good signal: if a use case is one line (return repository.getX()), skip it. If it has conditionals, combines data from two repositories, or has business rules โ€” keep it.

Code Examples

dartUse case with business logic
Output
// Two repositories, two business rules, one shuffle algorithm
// BLoC just calls StartQuizUseCase().call(config) and reacts

Common Mistakes

  • โœ—Putting business rules in the BLoC event handler โ€” they're untestable without Flutter
  • โœ—Creating use cases for every repository method โ€” use cases are for non-trivial operations only

Interview Tip

๐Ÿ’ก

The use case is the heart of the app. Ask yourself: 'If I switch from BLoC to Riverpod, does my business logic change?' With a proper use case layer, the answer is no.

#use-case#interactor#domain#business-logic