intermediate

Service Layer

Place application use-case orchestration in services when controllers, jobs, and consumers need the same business workflow.

Service layer hosts application use-case workflows shared by controllers, CLI jobs, and message consumers—place order, approve refund, sync inventory. It coordinates repositories, domain rules, transactions, and outbound integrations without becoming a second domain model stuffed with getters.

On interviews: place logic among controller, service, and entity. Discuss anemic domain versus rich domain and where orchestration legitimately lives.

Common pitfalls: thousand-line service classes; services that know HTTP details; duplicating the same workflow in controllers and workers.

The trade-off is flexibility versus complexity—know when the simpler path is enough.

Checklist:

  • One service method maps to a business use case.
  • Domain invariants enforced in entities or domain services.
  • Controllers stay thin translation layers.
  • Shared workflows extracted from duplicate entry points.