liveBook · Manning

Layered architecture

As explained in Grokking Software Architecture

A structural division of software with a defined responsibility and relationships to other divisions.

A growing application can become a junk drawer: user input, business rules, and database code all mixed together. Layered architecture applies separation of concerns across the whole application, grouping code into horizontal layers with distinct responsibilities. section 4.2 section 4.2.3

A common four-layer model divides the work like this:

  • Presentation displays information and receives user input. section 4.2.3
  • Application orchestrates use cases, coordinating domain operations and tasks such as transactions. section 4.2.3
  • Domain holds core business rules, logic, and state, independently of how they are presented or stored. section 4.2.3
  • Infrastructure handles external concerns such as databases and APIs. section 4.11
Figure 4.1 A visual look at Classic versus Modern Layered Architecture

Compare the classic and modern arrangements of application layers. section 4.2.3

The downward dependency rule keeps the stack orderly: each layer depends only on the layer directly beneath it. Layers are logical divisions of code, not necessarily separate machines; a tier is a physical deployment division. section 4.2 section 4.11

Some concerns, such as logging and security, cut across several layers. Depending on abstractions and using dependency inversion helps handle them without scattering concrete dependencies throughout the application. section 4.11

Watch for a fat controller paired with an anemic domain model: business logic has migrated out of the domain into presentation or application code. A rich domain model keeps data and the logic that operates on it together, making the system easier to test and maintain. section 4.11

Sketch a blog

For a blog that lists posts and accepts comments, draw the four layers and assign at least two components to each. For each component, ask whether it belongs to the business “why” or the infrastructure “how.” section 4.9.1 section 4.9.2

Split a layer when it becomes a junk drawer, team members repeatedly clash in the same files, or the right home for logic becomes hard to explain. Layering is a useful starting point for simple applications and prototypes; for complex rules or isolation from a database, compare it with hexagonal architecture and weigh the extra complexity. section 4.2.3 section 5.8.4

How Grokking Software Architecture covers it

How deep it goes
Defines four layers and dependency rules, addresses cross-cutting concerns and anti-patterns, and provides a sketching exercise.
Examples
Uses a blog-layer sketch, a repository swap, and a letter-delivery analogy for the OSI model.
What it assumes
Builds on separation of concerns and SOLID before applying them to whole-application structure.
What you can do after
Practice sketching application layers and assigning components by responsibility.
How it connects
Connects separation of concerns, dependency inversion, rich domain models, and hexagonal architecture.

Also explained in

Compare how 3 books explain layered architecture →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.