In a mobile workout app, when does a use-case layer between the screens and the repositories pay for itself, and when is it ceremony?
answer
- optional, not mandatory
- one rule, several screens
- combining repositories, tested alone
- pass-through use cases add files only
- cached heavy work lives in data
basics
~20 sA use case pays when a rule is reused by several screens, combines several repositories, or deserves its own tests, such as personal-record detection; a use case that only forwards one repository call is ceremony.
solid answer
~40 sIn a workout app, "is this set a personal record?" is needed by the logging screen for a badge, by the history charts to mark records, and by the weekly summary; it reads the set history and the exercise's rules. One `DetectPersonalRecord` use case gives every screen the same answer, tested once with fake repositories. "Get today's plan" is different: it is a single repository call, and wrapping it adds a class and a test with no new behaviour. Two refinements: a heavy computation whose result should be cached for several screens belongs in the data layer, not a stateless use case; and forcing *all* screen access through use cases buys one choke point at the cost of pass-through classes, which is worth it only when most access already goes through use cases.
go deeper
Recall that the domain layer is optional and that a use case performs one business action that screens can share.
Explain the signals that justify a use case, reuse, combining repositories and testability, and show a pass-through use case as the counter-example.
Place real rules from the app in the right layer, including heavy cached work in the data layer, and argue against a mirror of the repository surface with concrete costs.
Own the team-wide rule: when screens may call repositories directly, when access must go through use cases, and how code review keeps pass-through classes from accumulating.
## What a use-case layer is A **use case** (also called an interactor) is a small class in the **domain layer** that performs one business action, such as "detect a personal record" or "build the weekly summary". It sits between the presentation layer, which calls it, and the data layer's repositories, which it calls. Use cases are usually **stateless**: they hold no mutable data, take their inputs, call repositories and return a result. Mobile architecture guidance generally treats this layer as **optional**. The question an interviewer is asking is whether you can tell when it earns its cost, and that is a judgment about the specific app, not a rule. ## When it pays for itself - **Reuse across screens.** Personal-record detection is used by the logging screen, the history charts and the weekly summary. Written once as a use case, it cannot give three different answers. - **Combining repositories.** The weekly summary needs sets from the set repository and targets from the plan repository. Putting that join in a use case keeps both view models free of cross-repository logic. - **A rule worth testing alone.** Record detection has edge cases (equal weight with more reps, bodyweight exercises, unit changes). A use case tested with fake repositories pins them down without any UI. - **Another surface.** A summary shown in a notification or a companion device's UI can call the same use case without borrowing a screen's view model. - **A rule that keeps changing.** What counts as a record will be argued over by product for months. Keeping the definition in one class means each change is one edit and one set of tests, not a hunt through three screens. ## When it is ceremony - **Pass-through.** `GetTodaysPlan` that only returns `planRepository.observePlan(today)` adds a class, a file, a test and an injection binding, and no behaviour. - **One use case per repository method.** Mirroring the data layer one-to-one doubles the surface every change touches. - **A simple rule used once.** Logic used by a single screen and easy to read stays in that screen's state holder until a second caller appears. - **Layers added "in case".** A use case created because logic might arrive later costs files today; extracting one when the second caller arrives is a small, local refactor. ## Placing each rule in the workout app | Rule | Where it lives | Why | |---|---|---| | Detect a personal record | Use case | Shared by three screens, combines history and exercise rules, worth isolated tests | | Observe today's plan | Repository, called directly | One call; a use case would only forward it | | Weekly volume totals over a year of sets | Data layer | Heavy, and the result should be cached and shared rather than recomputed per caller | | Show weight as kg or lb | Presentation layer | Display formatting tied to the screen and the user's preference | | Build the weekly summary | Use case | Joins two repositories and applies targets | The third row is the subtle one. A computation that is expensive and reused should produce a cached result, and caching is state; the data layer, which already owns the store, is the place for it. A stateless use case would recompute it on every call. ## Deciding the team rule A team usually needs one rule everyone follows: 1. **Default:** add a use case when a rule is reused, combines repositories or needs its own tests; otherwise screens call repositories directly. 2. **Restricting access:** forbidding screens from calling repositories directly guarantees that domain logic, such as recording every data access, cannot be bypassed. The cost is a pass-through use case for every simple read. That trade is worth taking only once almost all access already goes through use cases. 3. **Review:** treat a use case with no logic as a code-review question, not a convention. ## Two constraints on any use case - **Safe to call from the UI thread.** If a use case does long-running work, it moves that work to a background thread itself, so callers never have to. - **No hidden state.** Results that must persist or be shared belong to repositories and the store, not to a use case's fields. The strong answer names a real rule that justifies a use case, names a real call that does not, and says where heavy cached computation goes instead.
- Once a domain layer exists, should screens be forbidden from calling repositories directly?It is a trade-off. The restriction stops screens bypassing domain logic, such as recording every data access, but forces a pass-through use case for every simple read. A common approach is to add use cases only when needed and to restrict direct access only when screens already reach data almost exclusively through use cases.
- The progress chart needs weekly volume totals over a year of sets, shown on three screens. Use case or repository?The data layer. The result is expensive and should be computed once and cached for all three screens, and caching is state that the repository or the store already owns. A stateless use case would recompute it for every caller, or would have to grow state it should not have.
- Should a use case keep the last result it computed, to save work next time?No. Keep use cases stateless, so they can be created per caller and invoked from any screen without shared mutable data. When a result needs caching, the cache belongs to the repository or the store, which already own the data's lifetime.
- Every repository method needs a matching use case for clean layering.
- A use-case layer is required for an app to be testable.
- A rule shared by several screens can be copied into each view model.
- Use cases should cache their results between calls to avoid recomputing.
- The domain layer is where server calls and store queries are made.