liveBook · Manning

Chapter 4: Thinking in Layers: Separating the 'How' from the 'Why' at Scale

What you will learn in chapter 4 of Grokking Software Architecture

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

What it builds on

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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

Concepts explained in this chapter

← Chapter 3: The Principles of Sound Structure: From Code That Works to Code That Lasts Chapter 5: Hexagonal Architecture: Protecting the Core: The Power of Ports and Adapters →

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.