Problem
Without structure, presentation, business, and data access code mix together, so changes in one concern force edits everywhere and nothing can be tested in isolation.
Also known as: n-tier-architecture, three-tier-architecture
Organize code into horizontal layers with distinct responsibilities where each layer depends only on the one below.
Without structure, presentation, business, and data access code mix together, so changes in one concern force edits everywhere and nothing can be tested in isolation.
Layered architecture is the default shape of many enterprise applications. Each layer has a role, and the dependency direction is top to bottom. It is easy to learn and works well until the domain grows richer or features need to evolve independently.
A request enters the presentation layer, moves through the service layer, uses domain rules, and reaches the data layer.
Presentation controllers, views
|
Application services, transactions
|
Domain entities, rules
|
Data access repositories, ORM
class OrderController { OrderService svc; }
class OrderService { OrderRepository repo; }
class OrderRepository { /* SQL */ }
A closed layer must be traversed; an open layer can be skipped. Marking a shared utility layer as open avoids useless delegation. Architecture tests can assert that no class in the data layer imports from presentation.
None — code structure
Low — single deployment typically
Low — familiar structure
Sinkhole anti-pattern where requests pass through layers doing nothing
Business logic leaking into controllers or SQL
Big ball of mud when layering rules are not enforced