Layered architecture
Give presentation, application, domain, and persistence distinct jobs.
The problem and the pattern
An endpoint should translate HTTP, a use case should coordinate an operation, and the domain should decide booking rules. Layers reduce the number of places a change must be understood. Keep dependencies pointed toward stable rules rather than letting transport types become domain inputs.
Follow the flow
Make it concrete
This excerpt isolates the concept. Connect it to the booking API as you work through the exercise.
public sealed class ReserveBooking
{
private readonly IBookingStore store;
public ReserveBooking(IBookingStore store) => this.store = store;
// Validate intent, apply rules, persist, then return a result.
}Trade-offs
- A layer can clarify ownership but adds navigation overhead.
- Layering does not require a pass-through class for every method.
- Test allowed dependency directions in the build.
When to avoid it
Avoid ceremonial layers that only forward arguments and provide no independent responsibility.
Your turn
A domain method returns an IActionResult. What boundary has leaked?
Reveal a worked answer
The domain depends on the web transport. Return a domain result or error and let the endpoint choose its HTTP representation.