ADR-002Where should a business rule live?
AcceptedContext
An order's quantity must be greater than zero. The rule has to hold when orders are created through the API — and later through a file import.
- Decision
- Keep the rule in OrderService. The controller only accepts the HTTP request and returns a response.
- Reason
- The rule lives in one place, behaves the same for every entry point and is covered by a unit test.
- Consequence
- The controller stays thin. Adding a new entry point doesn't mean rewriting the rule.
Options
AIn the controller
Pro: Quick to write.
Con: Every new entry point has to repeat the rule.
BIn the service layer
Pro: One place, works for every entry point, easy to test.
Con: One more layer.
COnly in the database (CHECK)
Pro: Bad data never gets stored.
Con: Hard to return a clear error to the user.