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
// 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.