4 Thinking in Layers: Separating the 'How' from the 'Why' at Scale
At a high level, the chapter shows how to scale the ideas of separation of concerns from individual classes to an entire application. It explains that layered architecture is a way to organize software into horizontal responsibilities so that the user interface, application orchestration, business rules, and external systems do not become a tangled mess. The central idea is that architecture should make it easy to understand where code belongs, why it exists, and how changes can be made without causing widespread damage.
The most important structural rule is downward dependency: higher layers may depend on lower ones, but lower layers must never know about higher ones. In practice, this means command flow moves downward while data can flow back upward, allowing lower layers to return results without creating reverse source-code dependencies. The chapter also distinguishes logical layers from physical tiers, describes how to split coarse layers into more focused ones when responsibilities blur, and emphasizes that modern systems often refine the classic three-layer model into presentation, application, domain, and infrastructure. It also notes that layers are a logical design choice, while tiers are a deployment choice.
The chapter then focuses on what can go wrong and how to prevent it. Cross-cutting concerns such as logging, authentication, and caching should not be scattered through business code or tangled into controllers; instead, they are handled through shared abstractions, dependency inversion, or sometimes AOP-style weaving. The chapter warns against anti-patterns like the fat controller and anemic domain model, showing that a healthier design keeps business logic inside a rich domain model while controllers and services stay thin and focused on orchestration. To make this architecture durable, it recommends physically enforcing boundaries through folders or separate projects, and verifying them with automated fitness functions so that the intended structure remains intact as the codebase grows.
A visual look at Classic versus Modern Layered Architecture
Downward Dependency Visual showing dependencies only flowing in one direction.
The lower layers only accept tasks from the upper layers; they don’t assign them. Data can flow both ways, though.
In a clean architecture, dependencies must only flow downward to the adjacent layer. Breaking this rule by "skipping" layers or creating upward dependencies (e.g., the Infrastructure Layer talking directly to the UI) creates a tangled web that makes the system impossible to test or modify without breaking everything.
Cross-Cutting Concerns serve multiple layers in an application.
The Four-Layer Diagram Blueprint with Cross-Cutting Concerns Showing Command and Data Flows
Fat Controllers and Anemic Domain Models are two of the most common anti-patterns!
Grandma's Letter visual showing how each represents a layer.
An example of what this sketch might look like on your whiteboard or piece of paper
Recap
- Layered Architecture is an architectural pattern that applies Separation of Concerns to an entire application by grouping code into horizontal layers based on responsibility.
- Modern applications often use a four-layer model: Presentation (the UI), Application (orchestration), Domain (core business rules), and Infrastructure (database, APIs, etc.).
- The non-negotiable "golden rule" is the Downward Dependency Rule, which states that layers must depend only on layers below them.
- Layers are a logical concept for organizing code, while Tiers are a physical concept for deploying code on different machines.
- Cross-Cutting Concerns (like logging or security) are handled by "cutting across" layers. We manage them cleanly by depending on abstractions (like ILogger) and using Dependency Inversion.
- The most common layering anti-pattern is the "Fat Controller / Anemic Domain Model" combo, where all business logic is pulled out of the domain and into the presentation/application layer.
- The solution is a "Rich Domain Model", where data and the logic that operates on it are encapsulated together, leading to a system that is far more testable and maintainable.
- This concept is battle-tested and mirrors the 7-layer OSI model that has successfully run the internet for decades.
FAQ
What is layered architecture in software design?
Layered architecture is a way of organizing an application into horizontal layers, each with a distinct responsibility. Common layers include Presentation, Application, Domain, and Infrastructure. It helps apply Separation of Concerns at the whole-system level.
What does the downward dependency rule mean?
The downward dependency rule says that an upper layer may depend on the layer below it, but a lower layer must never depend on a higher layer. This keeps dependencies flowing in one direction, which reduces coupling and makes the system easier to maintain.
How is command flow different from data flow in layered architecture?
Command flow goes from top to bottom: for example, the UI sends a request to the Application layer, which then coordinates the Domain and Infrastructure layers. Data flow can move in both directions, since lower layers can return results upward, but the source code dependency still must stay downward.
What are the responsibilities of the Presentation, Application, Domain, and Infrastructure layers?
The Presentation layer handles UI and user input. The Application layer orchestrates use cases and coordinates work. The Domain layer contains core business rules and state. The Infrastructure layer handles technical details like databases, email, APIs, and other external services.
Why do modern systems often split the classic Business Logic layer into more layers?
Because a single business logic layer can become too broad and turn into a “junk drawer.” Splitting it into more granular layers improves cohesion, makes responsibilities clearer, and helps teams scale without stepping on each other’s work.
What is the difference between a layer and a tier?
A layer is a logical separation in code based on responsibility. A tier is a physical separation in deployment, such as different machines or processes. You can have multiple layers on one tier, or distribute them across several tiers.
What is a cross-cutting concern?
A cross-cutting concern is a responsibility like logging, authentication, caching, or error handling that is needed across multiple layers. It “cuts across” the normal architecture and should be handled without creating tight coupling between layers.
How should cross-cutting concerns like logging be handled cleanly?
A common solution is to inject a shared abstraction, such as an ILogger interface, into the layers that need it. This keeps concerns separated and avoids tangled dependencies. In some systems, AOP or other infrastructure patterns may also handle this automatically.
What are the Fat Controller and Anemic Domain Model anti-patterns?
The Fat Controller happens when presentation or service classes take on too many responsibilities, such as validation, business rules, persistence, and email sending. The Anemic Domain Model happens when the domain objects become passive data holders with no real behavior.
Why is a rich domain model better than an anemic one?
A rich domain model keeps business rules inside the domain objects where they belong. This improves cohesion, makes logic easier to test in isolation, reduces the blast radius of changes, and keeps controllers and services thin and focused on orchestration.
Grokking Software Architecture ebook for free