Chapter 4: Thinking in Layers: Separating the 'How' from the 'Why' at Scale
This chapter introduction previews the topics to be covered: architectural layers, downward dependencies, cross-cutting concerns, anti-patterns, and diagrams.
After this chapter you will be able to
- Layering at Application Scale. Explain that architectural layers extend separation of concerns from individual components to the structure of an application, helping manage complexity and making the application easier to understand, test, and evolve.
- Application Scale Separation of Concerns. Explain how layered architecture applies separation of concerns to an entire application by grouping classes into horizontal layers according to their high-level responsibilities.
- Directly Downward Layer Dependencies. State the dependency rule for layered architecture and explain how it supports independent parts and a modular codebase.
- Command and Data Flow. Distinguish the downward direction of commands and source-code dependencies from the bidirectional movement of data. Explain why returning order data to the UI does not mean the Business Layer depends on the UI.
- Signals for Splitting Layers. Explain when a broad layer should be subdivided and why doing so can improve maintainability.
- Four Layer Responsibilities. Describe what presentation, application, domain, and infrastructure each handle, especially the distinction between coordinating a use case and defining its business rules.
- Logical Layers Versus Physical Tiers. Distinguish code-organization layers from deployment tiers and explain why adding servers for scale does not necessarily add tiers.
- Core Focus of Newer Patterns. Distinguish the broad aim of newer patterns from strict horizontal layering: they often focus on dependencies pointing inward toward the core domain and on separating business logic from concerns such as the UI and database.
- Downward Dependency Rule. Explain that upper layers may depend on lower layers, but lower layers must not depend on upper ones. Describe how this restriction preserves separation rather than letting the layers become tangled.
- Downward Dependency Benefits. Explain how one-way dependencies support separation of concerns, loose coupling, maintainability, and containment of code changes or failures.
- Replaceable UI And Infrastructure. Distinguish why the UI can be replaced without changing core business logic from why a database implementation can be replaced without the Application Layer knowing.
- Closed And Open Layer Tradeoff. Distinguish closed request paths from open-layer bridges and explain when the section says a bridge is justified.
- Costs of Upward Dependencies. Explain what happens when the Infrastructure Layer directly updates the UI and why that shortcut harms the system.
- Upward Data Without Upward Dependencies. Trace how a lower layer can return a result to the UI without calling the UI directly.
- Risks of Skipping Layers. Explain why a UI button calling the database directly violates the layered rule despite pointing downward.
- Visible Layer Boundaries. Explain why layers should be reflected in the physical structure of the codebase instead of existing only in developers’ heads.
- Four Layer Folder Mapping. Describe how a simple application's folders represent the Presentation, Application, Domain, and Infrastructure layers, and state what each contains or does.
- Decoupled Same Layer Siblings. Explain why folders or projects in the same layer should not depend on each other and identify which layer coordinates them when they must work together.
- Separate Projects for Layers. Describe how a larger C# or Java application can put each layer in its own project and separate library, with Domain, Application, Infrastructure, and Presentation holding different kinds of code.
- Compile-Time Dependency Enforcement. Explain how project references enforce the Downward Dependency Rule: Application may reference Domain, but Domain has no reference back to Application, so forbidden uses fail to compile.
- Architectural Fitness Functions. Explain how a fitness function checks code against architectural requirements, why it differs from a unit test, and how running it in the build process enforces layer boundaries.
- Cross-Cutting Concerns Across Layers. Explain what a cross-cutting concern is and give examples of responsibilities that multiple application layers need.
- Code tangling and responsibility. Explain how putting logging logic inside an order-saving method tangles a cross-cutting concern with business logic and violates the Single Responsibility Principle.
- Code scattering and change cost. Describe how repeating logging code across methods and layers makes a change of logging service difficult.
- Logger dependency inversion. Explain why OrderService’s direct use of a static Log class is rigid and hard to test, and how accepting an injected object that fulfills a Logger contract changes that dependency.
- Injected logging and layer boundaries. Explain how injecting a logging dependency keeps OrderService focused on business logic while leaving logging to FileLogger. Describe how this supports loose coupling and the Downward Dependency Rule in spirit.
- Logger injection tradeoff benefits. Justify the small setup cost of an interface and constructor by explaining how the design makes loggers replaceable and OrderService easier to unit test.
- Four Horizontal Layers. Describe the four layers in the blueprint, from Presentation at the top through Application and Domain to Infrastructure at the bottom, and explain that the stack represents separation of concerns.
- Commands Dependencies and Data. Distinguish downward command and dependency flow from bidirectional data flow: higher layers initiate commands toward lower layers, while input goes down and results can return up.
- Anti-patterns in layered design. Explain what an anti-pattern is and why an application can be hard to maintain despite having layers.
- Fat controller responsibilities. Explain how a controller or application service becomes a fat controller by taking on work outside its proper layer. Identify the controller’s HTTP-handling job and the unrelated validation, business logic, data access, and email responsibilities in the example.
- Costs of mixed responsibilities. Explain why putting those responsibilities together makes the discount logic hard to test in isolation, increases the risk that one change breaks another behavior, and makes the database or email provider hard to replace.
- Anemic Order Model Consequences. Explain why moving order logic into a controller makes the Order class an anemic domain model, and how that weakens encapsulation and cohesion.
- Rich Order Domain Model. Explain why the Order class owns the total-price and discount rules, and how a calculated total keeps the price consistent with its items.
- Domain Owned Repository Contract. Explain why IOrderRepository is defined in the Domain Layer and implemented by Infrastructure, including the difference between source-code dependency and control flow.
- Refactoring Benefits And Tradeoff. Justify the extra classes by describing how the separation improves isolated testing, makes changes easier to locate, and limits the effect of a pricing bug.
- Intentional placement of responsibilities. Explain where core business rules, orchestration, HTTP/Web concerns, and database/email concerns belong, and why placement is an architectural decision rather than a random movement of code.
- Automated enforcement of boundaries. Explain why layer boundaries need protection over time and describe the kind of automated check the section calls for.
- Layering Beyond Application Code. Explain that layering is not just a modern way to organize application code; the pioneers who built the internet also thought in layers.
- Application and cable coupling. Describe how making an application handle every part of communication caused even a physical cable change to require application changes.
- OSI separation of concerns. Explain why the OSI Model divides sending data into seven layers, using the letter analogy to illustrate separate responsibilities.
- Delivery layer responsibilities. Distinguish the Transport, Network, Data Link, and Physical layers by how the letter reaches Grandma.
- Replaceability through layered separation. Explain how separating layers lets one part change without forcing changes in others, using both the OSI cable swap and the application repository swap.
- Four-Layer Component Sketch. Describe how to draw the Presentation, Application, Domain, and Infrastructure layers, place at least two specific classes or files in each, and check whether a component handles the “how” of Infrastructure or the “why” of Domain.
- The Infrastructure Anchor. Explain why a traditional layered system can still leave core logic tied to infrastructure, even when its four layers are well structured.
- Layer Responsibilities and Tiers. Describe how layered architecture groups code by responsibility, name the four common layers and their roles, and distinguish logical layers from physical deployment tiers.
- Downward Dependencies and Shared Concerns. State the Downward Dependency Rule and explain how abstractions and Dependency Inversion help handle concerns such as logging or security that cut across layers.
- Rich Domain Versus Fat Controller. Explain the Fat Controller / Anemic Domain Model anti-pattern and how a Rich Domain Model addresses it.
What it builds on
- Separation of Concerns (chapter 3) · The principle of separation of concerns that layering applies at a larger scale.
- Zooming Out: The Fractal Nature of Good Architecture (chapter 3) · Applies the idea that sound design principles recur at different scales.
- Cohesion and Coupling (chapter 3) · Loose coupling
- Testability in Action: Breaking Dependencies (chapter 2) · Testability through breaking dependencies
- D – Dependency Inversion Principle (DIP) (chapter 3) · Dependency Inversion Principle
- S – Single Responsibility Principle (SRP) (chapter 3) · The Single Responsibility Principle.
- Maintainability in Action: From Mess to Clarity (chapter 2) · Maintainability as a quality of code that must be changed.
- The “-ilities”: Your Vocabulary of Quality (chapter 2) · The quality attributes used to evaluate the tradeoff.
Section by section
Introduction
This chapter introduction previews the topics to be covered: architectural layers, downward dependencies, cross-cutting concerns, anti-patterns, and diagrams.
Introduction
Introduces architectural layering as a way to apply separation of concerns across an entire application rather than only within individual components.
- Layering at Application Scale: Explain that architectural layers extend separation of concerns from individual components to the structure of an application, helping manage complexity and making the application easier to understand, test, and evolve.
Why it matters: It connects the structural principles used within components to decisions about organizing a whole application.
Key terms: architectural layers, Separation of Concerns
4.2 Layered Architecture
Introduces layered architecture as separation of concerns at application scale, with distinct responsibilities and a strict rule for dependencies between layers.
- Application Scale Separation of Concerns: Explain how layered architecture applies separation of concerns to an entire application by grouping classes into horizontal layers according to their high-level responsibilities.
Why it matters: It gives you a way to keep responsibilities distinct even as an application grows beyond a few classes. - Directly Downward Layer Dependencies: State the dependency rule for layered architecture and explain how it supports independent parts and a modular codebase.
Why it matters: The dependency rule makes the separation between layers more than a naming convention.
Key terms: Layered Architecture, Separation of Concerns (SoC), horizontal layers, strict dependency, layer directly beneath it, modular codebase
4.2.1 Command Flow vs. Data Flow
Distinguishes downward command and dependency direction from the bidirectional movement of data between layers.
- Command and Data Flow: Distinguish the downward direction of commands and source-code dependencies from the bidirectional movement of data. Explain why returning order data to the UI does not mean the Business Layer depends on the UI.
Why it matters: It keeps a returned result from being mistaken for a reversed dependency.
Key terms: Command Flow, Data Flow, Dependency, UI Layer, Business Layer
4.2.2 Classic Layered Architecture
4.2.3 The Evolution of Layers
This section explains why growing applications may need more focused layers, what the four common layers do, and how logical layers differ from physical tiers.
- Signals for Splitting Layers: Explain when a broad layer should be subdivided and why doing so can improve maintainability.
Why it matters: These signals help you decide whether another layer clarifies responsibilities rather than merely adding structure. - Four Layer Responsibilities: Describe what presentation, application, domain, and infrastructure each handle, especially the distinction between coordinating a use case and defining its business rules.
Why it matters: Clear roles make it easier to place code without turning one layer into a junk drawer. - Sibling Infrastructure Sub Layers: Explain how a five-layer variation separates database access from other external communications without making one pass through the other.
Why it matters: The extra separation should clarify external responsibilities, not create an unnecessary route through the database. - Logical Layers Versus Physical Tiers: Distinguish code-organization layers from deployment tiers and explain why adding servers for scale does not necessarily add tiers.
Why it matters: Keeping code structure separate from deployment structure prevents misleading architecture descriptions.
Key terms: Junk Drawer Effect, Team Scaling, Blurry Boundaries, Separation of Concerns, Maintainability, Presentation Layer, Application Layer, Domain Layer, Infrastructure Layer, Data Access Layer (DAL)
4.2.4 Challenging the Layers: Newer Patterns
Contrasts classic horizontal layering with newer patterns that emphasize protecting core business logic from external concerns.
- Core Focus of Newer Patterns: Distinguish the broad aim of newer patterns from strict horizontal layering: they often focus on dependencies pointing inward toward the core domain and on separating business logic from concerns such as the UI and database.
Why it matters: It helps you recognize why architects might look beyond classic layers without assuming that layering is obsolete.
Key terms: layered architecture, core domain, core business logic, Hexagonal Architecture, Clean Architecture, Onion Architecture
4.3 Downward Dependency
This section defines the Downward Dependency Rule and explains why one-way dependencies keep logical layers from becoming tangled.
- Downward Dependency Rule: Explain that upper layers may depend on lower layers, but lower layers must not depend on upper ones. Describe how this restriction preserves separation rather than letting the layers become tangled.
Why it matters: The rule makes layers more than labels by protecting the separation they are meant to provide.
Key terms: Downward Dependency Rule, layered architecture, dependencies
4.3.1 Why Is It So Important?
Explains why downward dependencies make layers easier to maintain, test, and replace, while distinguishing source-code isolation from data validation and closed layers from justified open layers.
- Downward Dependency Benefits: Explain how one-way dependencies support separation of concerns, loose coupling, maintainability, and containment of code changes or failures.
Why it matters: One-way dependencies make the effects of changes easier to understand and contain. - Isolated Layer Testing: Describe how a layer can be tested without its real UI or database by supplying mock or fake implementations of interfaces it depends on.
Why it matters: Testing a layer without real external components makes its behavior easier to check independently. - Replaceable UI And Infrastructure: Distinguish why the UI can be replaced without changing core business logic from why a database implementation can be replaced without the Application Layer knowing.
Why it matters: The two kinds of replaceability show how layer boundaries protect business logic from technology choices. - Layer Boundaries And Data Validation: Explain why strict layering does not remove the need to validate incoming data at every boundary, with the Domain Layer as the final defense.
Why it matters: A well-layered codebase can still end up with invalid state if incoming data is trusted. - Closed And Open Layer Tradeoff: Distinguish closed request paths from open-layer bridges and explain when the section says a bridge is justified.
Why it matters: Skipping a layer may simplify a read, but doing so carelessly can bypass important rules.
Key terms: downward dependency, Separation of Concerns, Loose Coupling, public interface, blast radius, Enhanced Testability, Domain Layer, mock, fake, Infrastructure interfaces
4.3.2 The Danger Zone: Violating the Rule
This section shows why upward dependencies and skipped layers undermine layered architecture, then introduces ways to handle upward data flow and concerns shared across layers without tangling dependencies.
- Costs of Upward Dependencies: Explain what happens when the Infrastructure Layer directly updates the UI and why that shortcut harms the system.
Why it matters: A convenient direct call can make layers harder to change and test independently. - Upward Data Without Upward Dependencies: Trace how a lower layer can return a result to the UI without calling the UI directly.
Why it matters: Returning a result preserves layer boundaries while still letting the interface reflect what happened. - Risks of Skipping Layers: Explain why a UI button calling the database directly violates the layered rule despite pointing downward.
Why it matters: A downward shortcut can silently bypass business and security rules while making the UI brittle. - Shared Cross-Cutting Services: Describe the shared-service approach introduced for concerns needed by multiple layers.
Why it matters: A concern used throughout an application need not force layers to call across their boundaries. - AOP Weaving and Limits: Describe what AOP weaving adds to code and distinguish uniform logging from contextual logging.
Why it matters: AOP can remove repetitive uniform code, but it is not a substitute for logging meaningful business context.
Key terms: Infrastructure Layer, PresentationLayer, upward dependency, Separation of Concerns, Testability, circular dependency, Application Layer, Application Service, Presentation Layer, Data Flow
4.4 Physically Structuring Layers
The section explains how to make layers visible in the codebase and enforce their dependency boundaries rather than relying on developers to remember them.
- Visible Layer Boundaries: Explain why layers should be reflected in the physical structure of the codebase instead of existing only in developers’ heads.
Why it matters: Visible boundaries help developers follow the architecture without relying on memory alone. - Automated Dependency Checks: Describe how folder-based projects can automatically flag forbidden upward dependencies during local builds or CI/CD.
Why it matters: Automated checks catch boundary violations without depending on everyone’s discipline.
Key terms: layers, physical structure of your codebase, Downward Dependency Rule, static analysis tools, dependency graph visualization tools, upward dependencies, local build checks, CI/CD pipeline
4.4.1 Approach 1: Folders (common in Python/JavaScript)
Shows how folders can represent four architectural layers and why components within the same layer should remain decoupled.
- Four Layer Folder Mapping: Describe how a simple application's folders represent the Presentation, Application, Domain, and Infrastructure layers, and state what each contains or does.
Why it matters: A folder layout can show where different kinds of code belong. - Decoupled Same Layer Siblings: Explain why folders or projects in the same layer should not depend on each other and identify which layer coordinates them when they must work together.
Why it matters: Separating siblings prevents a shared layer from becoming a web of direct dependencies.
Key terms: Presentation Layer, Application Layer, Domain Layer, Infrastructure Layer, siblings, decoupled, Infrastructure
4.4.2 Approach 2: Separate Projects (Common in Java/C#)
This section shows how separate projects can make layer boundaries compile-time rules and briefly contrasts layer-based organization with feature-based vertical slices.
- Separate Projects for Layers: Describe how a larger C# or Java application can put each layer in its own project and separate library, with Domain, Application, Infrastructure, and Presentation holding different kinds of code.
Why it matters: Project boundaries make the intended separation of responsibilities visible in the application’s structure. - Compile-Time Dependency Enforcement: Explain how project references enforce the Downward Dependency Rule: Application may reference Domain, but Domain has no reference back to Application, so forbidden uses fail to compile.
Why it matters: The compiler can catch a layer-boundary violation before the application runs. - Vertical Slice Grouping: Distinguish grouping code horizontally by technical layer from grouping it vertically by feature, while noting that a feature slice can still contain layers.
Why it matters: Feature-based grouping is a different way to arrange code, not necessarily an abandonment of layers.
Key terms: separate projects, Domain, Application, Infrastructure, Presentation, Downward Dependency Rule, project references, compile, Vertical Slice Architecture, layers
4.4.3 Fitness Functions: A Different Kind of Test
This section introduces fitness functions as automated checks that enforce architectural boundaries when a compiler cannot.
- Architectural Fitness Functions: Explain how a fitness function checks code against architectural requirements, why it differs from a unit test, and how running it in the build process enforces layer boundaries.
Why it matters: It gives a team a way to enforce architectural boundaries rather than relying on developers to remember them.
Key terms: Fitness Function, Unit Test, architectural requirements, build process
4.5 Cross-Cutting Concerns
Introduces cross-cutting concerns as technical responsibilities needed across multiple application layers.
- Cross-Cutting Concerns Across Layers: Explain what a cross-cutting concern is and give examples of responsibilities that multiple application layers need.
Why it matters: Recognizing a shared responsibility helps you see why it does not fit neatly within a single layer.
Key terms: Cross-Cutting Concern, layers, logging, authentication, caching, error handling
4.5.1 The Problem: Tangling and Scattering
This section explains how mixing logging with business logic and repeating logging code across layers undermine separation of concerns.
- Code tangling and responsibility: Explain how putting logging logic inside an order-saving method tangles a cross-cutting concern with business logic and violates the Single Responsibility Principle.
Why it matters: Spotting tangling helps you see when a business method has taken on an unrelated job. - Code scattering and change cost: Describe how repeating logging code across methods and layers makes a change of logging service difficult.
Why it matters: Recognizing scattered code makes the cost of an otherwise simple logging change visible.
Key terms: Code Tangling, Single Responsibility Principle, business logic, Code Scattering, Maintainability, blast radius
4.5.2 The Solution: Invert the Dependency!
This subsection shows how injecting a logger through a contract removes OrderService’s direct dependence on a static logging class.
- Logger dependency inversion: Explain why OrderService’s direct use of a static Log class is rigid and hard to test, and how accepting an injected object that fulfills a Logger contract changes that dependency.
Why it matters: A contract lets the service use logging without being tied to one logger.
Key terms: OrderService, Log, Logger contract, Dependency Inversion (DIP), inject
4.5.3 The Payoff: Why This Matters Architecturally
Explains why injecting logging as a cross-cutting dependency is worth the setup complexity, and notes that AOP can automate the same separation.
- Injected logging and layer boundaries: Explain how injecting a logging dependency keeps OrderService focused on business logic while leaving logging to FileLogger. Describe how this supports loose coupling and the Downward Dependency Rule in spirit.
Why it matters: Separating logging from business logic helps preserve clear responsibilities and layer boundaries. - Logger injection tradeoff benefits: Justify the small setup cost of an interface and constructor by explaining how the design makes loggers replaceable and OrderService easier to unit test.
Why it matters: The modest setup cost makes logging changes and business-logic tests less disruptive. - AOP as automated separation: Describe AOP as a framework technique that can weave logging or transaction logic between method calls while preserving the separation between business logic and technical concerns.
Why it matters: AOP changes how the separation is implemented, not why logging is kept out of business logic.
Key terms: cross-cutting concerns, injected dependencies, DIP, SRP, Downward Dependency Rule, OrderService, interface, constructor, FileLogger, CloudLogger
4.6 The Four-Layer Diagram
Introduces a diagram that brings together the four-layer architecture, dependency rule, and cross-cutting concerns.
4.6.1 Reading the Blueprint
Explains how to read the four-layer blueprint: its stacked responsibilities, downward commands and dependencies, bidirectional data, and cross-cutting services.
- Four Horizontal Layers: Describe the four layers in the blueprint, from Presentation at the top through Application and Domain to Infrastructure at the bottom, and explain that the stack represents separation of concerns.
Why it matters: The stack gives you a starting point for reading the rest of the blueprint. - Commands Dependencies and Data: Distinguish downward command and dependency flow from bidirectional data flow: higher layers initiate commands toward lower layers, while input goes down and results can return up.
Why it matters: Distinguishing a returned result from an upward command prevents misreading the dependency arrows. - Cross-Cutting Concerns Bar: Explain what the vertical bar represents and why services such as Logging, Security, and Caching are shown as accessible across the horizontal layers.
Why it matters: It shows where shared responsibilities fit without treating them as upward commands between layers.
Key terms: Presentation, Application, Domain, Infrastructure, Separation of Concerns, Downward Dependency Rule, Command Flow, Data Flow, dependencies, Cross-Cutting Concerns
4.7 Anti-Patterns: When Layers Go Wrong
This section explains why having layers does not guarantee good design and defines the anti-patterns that can undermine it.
- Anti-patterns in layered design: Explain what an anti-pattern is and why an application can be hard to maintain despite having layers.
Why it matters: Layers help only when the design within them avoids shortcuts that create technical debt.
Key terms: anti-pattern, pattern, layers, technical debt
4.7.1 Anti-Pattern 1: The Fat Controller (or Bloated Service)
This section diagnoses the fat controller anti-pattern, explains its costs, and shows how cyclomatic complexity can help flag risky methods without becoming a rigid rule.
- Fat controller responsibilities: Explain how a controller or application service becomes a fat controller by taking on work outside its proper layer. Identify the controller’s HTTP-handling job and the unrelated validation, business logic, data access, and email responsibilities in the example.
Why it matters: Spotting misplaced responsibilities helps you recognize when apparently working code has weak layer boundaries. - Costs of mixed responsibilities: Explain why putting those responsibilities together makes the discount logic hard to test in isolation, increases the risk that one change breaks another behavior, and makes the database or email provider hard to replace.
Why it matters: These costs explain why a working endpoint can still be a brittle architectural choice. - Cyclomatic complexity as signal: Describe cyclomatic complexity as a count of possible paths through a function, increased by branches and loops. Explain how automated scores can flag methods for investigation without treating a high score as an automatic command to split cohesive logic.
Why it matters: The metric can draw attention to risky code while leaving room for judgment about cohesive complexity.
Key terms: Fat Controller, Presentation Layer, Application Layer, SoC, SRP, low cohesion, testability, blast radius, replaceability, tightly coupled
4.7.2 Anti-Pattern 2: The Anemic Domain Model
This section explains how moving order behavior into a fat controller leaves an anemic domain model that cannot protect its own data.
- Anemic Order Model Consequences: Explain why moving order logic into a controller makes the Order class an anemic domain model, and how that weakens encapsulation and cohesion.
Why it matters: It helps you spot when a class stores data while other classes make the decisions that should protect it.
Key terms: Anemic Domain Model, Fat Controller, encapsulation, cohesion, Order
4.7.3 “After”: The Defensible Decision (Rich Domain, Thin Controller)
Refactors order handling into a rich domain model with thin outer layers and domain-owned contracts, then explains the dependency tradeoff and its benefits.
- Rich Order Domain Model: Explain why the Order class owns the total-price and discount rules, and how a calculated total keeps the price consistent with its items.
Why it matters: Putting the rules in Order gives changes to pricing one clear home. - Domain Owned Repository Contract: Explain why IOrderRepository is defined in the Domain Layer and implemented by Infrastructure, including the difference between source-code dependency and control flow.
Why it matters: The domain can describe what persistence it needs without depending on SQL. - Thin Outer Layer Roles: Describe how the refactor divides responsibilities among the Application, Presentation, and Infrastructure Layers rather than putting business rules in the controller or service.
Why it matters: Clear layer roles keep request handling and external-service details from swallowing business logic. - Refactoring Benefits And Tradeoff: Justify the extra classes by describing how the separation improves isolated testing, makes changes easier to locate, and limits the effect of a pricing bug.
Why it matters: The added structure is defensible when it makes changes and tests safer.
Key terms: rich domain model, Calculated Property, Atomic Truth, Gold Discount, IOrderRepository, Domain Layer, Infrastructure Layer, Dependency Inversion Principle (DIP), control flow, Application Layer
4.7.4 The Architect’s Mandate: Decide and Enforce
This section explains how architects assign responsibilities to layers and use automated checks to keep those boundaries intact.
- Intentional placement of responsibilities: Explain where core business rules, orchestration, HTTP/Web concerns, and database/email concerns belong, and why placement is an architectural decision rather than a random movement of code.
Why it matters: Choosing where code belongs helps prevent responsibilities from accumulating in the wrong layer. - Automated enforcement of boundaries: Explain why layer boundaries need protection over time and describe the kind of automated check the section calls for.
Why it matters: Automated checks help a sound layer design survive everyday changes to the code.
Key terms: Domain Model, Application Layer, Presentation Layer, Infrastructure Layer, Fat Controller, Anemic Domain, entropy, Fitness Functions, Automated Architectural Enforcement
4.8 The Dork Side: The 7-Layer Sandwich That Runs the Internet
This section connects logical layers in applications to a much older use of layering in computing and the early internet.
- Layering Beyond Application Code: Explain that layering is not just a modern way to organize application code; the pioneers who built the internet also thought in layers.
Why it matters: It connects the layers used in app design to a broader computing idea.
Key terms: logical layers, layers
4.8.1 The Problem: The Digital Tower of Babel
This subsection shows how proprietary networking and tightly coupled applications made early computers hard to connect and change.
- Proprietary networking barriers: Explain why computers from different manufacturers could not readily communicate in the example from the 1970s.
Why it matters: It establishes the communication problem that the next subsection addresses. - Application and cable coupling: Describe how making an application handle every part of communication caused even a physical cable change to require application changes.
Why it matters: It shows the cost of mixing application concerns with low-level networking details.
Key terms: proprietary, monolithic, Fat Controller, tightly coupled
4.8.2 The Solution: "Grandma's Letter" (The OSI Model)
The section uses Grandma’s letter to show how the seven-layer OSI Model separates the job of sending data into distinct responsibilities.
- OSI separation of concerns: Explain why the OSI Model divides sending data into seven layers, using the letter analogy to illustrate separate responsibilities.
Why it matters: This gives you a way to reason about a complex communication task one responsibility at a time. - Upper layer responsibilities: Distinguish the Application, Presentation, and Session layers by their roles in Grandma’s letter.
Why it matters: These distinctions show that creating, presenting, and managing a communication are different jobs. - Delivery layer responsibilities: Distinguish the Transport, Network, Data Link, and Physical layers by how the letter reaches Grandma.
Why it matters: It helps you tell reliability, routing, local delivery, and the transmission medium apart.
Key terms: 7-Layer OSI Model, Separation of Concerns, Application Layer, Presentation Layer, Session Layer, Transport Layer (TCP), Network Layer (IP), Data Link Layer, Physical Layer, packets
4.8.3 The "Aha!" Moment
This section uses the OSI model and an application repository swap to show how loose coupling makes layers replaceable.
- Replaceability through layered separation: Explain how separating layers lets one part change without forcing changes in others, using both the OSI cable swap and the application repository swap.
Why it matters: The same separation that permits a network cable swap can help an application change its storage without disturbing higher-level work.
Key terms: OSI model, loose coupling, replaceability, Physical Layer, Domain Layer, Infrastructure Layer
4.9 Your “In Action” Mission: Sketching Layers
Introduces a five-minute exercise to sketch layers before writing code.
4.9.1 Your Mission: Sketch a Blog
Sets a design exercise for a simple blog that lists posts and lets users comment on them.
4.9.2 Instructions
This exercise asks the reader to sketch a four-layer architecture and place concrete components according to their responsibilities.
- Four-Layer Component Sketch: Describe how to draw the Presentation, Application, Domain, and Infrastructure layers, place at least two specific classes or files in each, and check whether a component handles the “how” of Infrastructure or the “why” of Domain.
Why it matters: Placing actual classes or files in the sketch helps turn layer names into a concrete design.
Key terms: Presentation, Application, Domain, Infrastructure, How, Why
4.9.3 Extra Credit!
Offers an optional challenge to sketch the layers of a banking transfer system, without explaining where its rules or gateways belong.
4.9.4 A Model Sketch (For Comparison)
The section offers a model layered sketch for comparison and identifies a dependency that remains even in a well-structured layered system.
- The Infrastructure Anchor: Explain why a traditional layered system can still leave core logic tied to infrastructure, even when its four layers are well structured.
Why it matters: A tidy layer diagram does not, by itself, remove the core’s dependence on database plumbing.
Key terms: Infrastructure Anchor, core logic, Downward Dependency Rule, plumbing
4.10 Checklist
This section provides a checklist for readers to review the chapter’s core ideas about layered architecture.
4.11 Recap
Recaps how layered architecture separates responsibilities, controls dependencies, handles cross-cutting concerns, and keeps business logic in the domain.
- Layer Responsibilities and Tiers: Describe how layered architecture groups code by responsibility, name the four common layers and their roles, and distinguish logical layers from physical deployment tiers.
Why it matters: This distinction helps you discuss code organization without confusing it with deployment. - Downward Dependencies and Shared Concerns: State the Downward Dependency Rule and explain how abstractions and Dependency Inversion help handle concerns such as logging or security that cut across layers.
Why it matters: These rules help preserve layer boundaries even when a concern is needed throughout an application. - Rich Domain Versus Fat Controller: Explain the Fat Controller / Anemic Domain Model anti-pattern and how a Rich Domain Model addresses it.
Why it matters: Where business rules live affects whether the domain remains clear, testable, and maintainable.
Key terms: Layered Architecture, Separation of Concerns, Presentation, Application, Domain, Infrastructure, Downward Dependency Rule, Cross-Cutting Concerns, Dependency Inversion, ILogger
Where this is used later
- The Clarity Engineer's Shift in Focus: Inside vs. Outside (chapter 5) · The earlier top-and-bottom view of layered architecture.
- Decoupling the Core: Breaking Free from "Database Gravity" (chapter 5) · Introduces the idea of separating the core from external concerns before the book examines that decoupling in depth.
- The “Database First” Trap (chapter 5) · Folders as one way to structure layers.
- Paying Off the Receipt: Architectural Fitness Functions (chapter 13) · Revisits architectural fitness functions as automated guardrails.
- Enforcing Boundaries in the Code (chapter 13) · The idea of testing architectural structure with fitness functions.
- Recap (chapter 13) · The idea of fitness functions as tests of architectural rules.
- Zero Trust Architecture: The Internal Guards (chapter 11) · Cross-cutting concerns that need not be embedded in core application logic.
- 2. The Ports (The Interfaces) (chapter 5) · Builds on using abstractions to separate a core from external concerns.
Concepts explained in this chapter
- layered architectureA structural division of software with a defined responsibility and relationships to other divisions.
- service layerThe architectural layer that coordinates application use cases and orchestrates interactions with domain and infrastructure components.
- presentation layerThe part of an application that displays information, captures user input, and translates user interactions into application requests.
- downward dependency ruleThe Downward Dependency Rule requires dependencies between architectural layers to point only toward lower-level layers and never upward.
- infrastructure layerThe architectural layer that implements external technical concerns such as persistence, messaging, and third-party communication.
- domain layerThe architectural part of an application that contains core business knowledge, rules, logic, state, entities, value objects, and domain services.
- fat controllerA fat controller is a controller design in which presentation-layer code accumulates excessive business logic and coordination responsibilities.
- four-layer architectureA four-layer architecture organizes an application into presentation, application, domain, and infrastructure layers with distinct responsibilities and dependencies.
- aspect-oriented programmingA programming technique that modularizes cross-cutting concerns and automatically applies them at designated join points in program execution.
- cross-cutting concernA software responsibility that spans multiple architectural layers rather than belonging to just one.
- rich domain modelA software design approach in which domain objects contain substantial business behavior and enforce domain rules rather than serving only as data containers.
- logical layerLogical separation organizes code into distinct responsibility-based groups without requiring separate deployment locations.
- physical separationPhysical separation deploys application parts onto different machines or processes that communicate across execution boundaries.
- layer skippingLayer skipping is a dependency shortcut in which a component bypasses one or more intervening architectural layers.
- upward dependencyAn upward dependency is a dependency from a lower software layer to a higher one.
- tierA tier is a physically deployable part of an application separated from other parts by a process or network boundary.
- anemic domain modelAn anemic domain model is a domain model whose objects contain little or no domain behavior, leaving rules in other components.
- dependency inversionThe Dependency Inversion Principle states that high-level and low-level modules should depend on abstractions, while abstractions should not depend on implementation details.
- separation of concernsSeparation of concerns is a design principle that reduces complexity by dividing a system into distinct parts with separate responsibilities and boundaries.
- data flowData flow is the movement of information between software layers or components, potentially in multiple directions.
- business logicBusiness logic is the application code that implements domain-specific rules and coordinates the system's behavior.
- logThe recording of software events, states, or diagnostic information for operational analysis.
- maintainabilityMaintainability is the degree to which software can be understood, corrected, modified, or extended efficiently over time.
- single responsibility principleThe single responsibility principle is the design principle that a software unit should have one clearly focused responsibility and one primary reason to change.
- testabilityTestability is the degree to which software can be easily tested to verify its behavior and correctness.
- tradeoffA tradeoff is a situation in which gaining a benefit requires accepting a cost, risk, or limitation elsewhere.
- agent orchestrationOrchestration is the coordination of multiple tasks or operations by a central service that directs when other services act.
- encapsulationEncapsulation is a design principle that keeps implementation details and process complexity inside the component responsible for them while restricting uncontrolled access to its state.
- hexagonal architectureAn architectural pattern that isolates the application core from external systems through ports and adapters.
- architectural fitness functionA fitness function is an automated check that evaluates whether a software system conforms to a desired architectural property or constraint.
- cohesionCohesion is the degree to which the responsibilities within a software component belong together.
- dependency injectionInjection is the process of providing a dependency to the object that needs it.
- monolithic architectureA software application developed and deployed as a single unified unit.
- dependencyA relationship in which one software component relies on another.
- sketchA sketch is an informal architecture diagram that represents a system’s main responsibilities and relationships without the formality of a UML diagram.
- circular dependencyA cycle in which components depend on one another, directly or through other components, forming a closed dependency path.
- data layerA data layer isolates data access and persistence operations from other application responsibilities.
- horizontal scalingIncreasing capacity by adding instances of a system component across multiple servers or processes.
- validationData validation is the enforcement of rules that determine whether stored values are valid and conform to required constraints.
- contractAn API contract specifies the operations, data, and behavioral expectations governing communication between an API and its clients.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
