Part 3 of 7 · 3 min read
The layered style
Group the code by technical job: screen, rules, storage.
Think of a restaurant staffed by role: waiters, cooks and dishwashers. A waiter never washes up directly; the plate goes through the chain. Each role knows only the one next to it.
FSA says the layered (n-tier) style is the default for most applications because it is simple, familiar and cheap. It also fits Conway's law: the organisation has UI developers, backend developers, rules developers and database specialists, and they match the layers.
Conway's law
Conway's law says that organisations that design systems tend to produce designs that copy the communication structure of the organisation.
Example: a company with a separate database team and a separate web team tends to build a separate database layer and a separate web layer.
The usual topology has four layers:
| Layer | Job |
|---|---|
| Presentation | Handle the user interface and browser communication |
| Business | Run the business rules for a request |
| Persistence | Turn business requests into database calls |
| Database | Store the data |
Small applications may merge business and persistence into three layers. Larger ones may have five or more. The layers can be deployed in different ways. They may be one deployment unit with an external database. The presentation layer may be its own unit. Or all four may be packed into one unit, which suits products installed at customer sites.
Each layer forms an abstraction. The presentation layer does not care how customer data is fetched. The business layer does not care how the data is shown. That separation of concerns makes roles clear, so a person can specialise. The trade-off, says the book, is a loss of overall agility.
Technical partitioning
Technical partitioning groups code by its technical role (screen, rules, storage), not by the business area it serves.
Example: all database code of the whole shop sits in one persistence layer.
The weakness is plain. A business area such as "customer" is spread through every layer: a customer screen, customer rules, customer storage. A change to customers touches all layers. The book adds that domain-driven design therefore fits badly with a layered style.
Closed and open layers. A closed layer means a request must pass through it to reach the layer below. An open layer may be skipped. The book explains the idea of layers of isolation: changes in one layer do not hurt others as long as the contracts between them stay the same. If the presentation layer could call the database directly, then a change to the persistence layer would hit both the business and presentation layers, and the application would be tightly coupled and brittle. The reward of closed layers is that a layer can be replaced. The book's example is swapping an old JavaServer Faces front end for React without touching other layers.
Skipping layers is tempting. For a simple read, the presentation layer going straight to the database is faster. The book notes this was once called the fast-lane reader pattern. The price is loss of isolation.
Sometimes a layer should be open on purpose. Suppose shared helpers (logging, dates, auditing) live in the business layer, and the architecture forbids the presentation layer from using them. Because the business layer is closed to the presentation layer yet contains these helpers, the rule is hard to enforce. The fix in the book is a new services layer that holds the shared objects, marked open so that the business layer can use it or skip it. The lesson: document which layers are open and why, or the architecture drifts into tight coupling.
Check yourself:
- Why does a feature about "customers" cross every layer in a layered style?
- What is the benefit of keeping a layer closed?