architect-handbook

Software Architect Handbook

View on GitHub

Martin Fowler: The Three Principal Layers

A single application can often have multiple packages of each of these three subject areas.

Sometimes the layers are arranged so that the domain layer completely hides the data source from the presentation. More often, however, the presentation access the data store directly. While this is less pure, it tends to work better in practice. The presentation may interpret a command from the user, use the data source to upull the relevant data out of the database, and then let the domain logic manipulate that data before presenting it.

The domain and data source should never be dependent on the presentation. That is, there should be no subroutine call from the domain or data source code into the presentation code. This rule makes it easier to substitute different presentations on the same foundation and makes it easier to modify the presentation without serios ramifications deeper down.

Presentation is an external interface for a service your system offers to someone else, whether it be a complex human or a simple remot program. Data source is the interface to things that are providing a service to you.

Although we can identify the three common responsiblity layers of presentation, domain, and data source for every enterprise application, how you separate them depend depends on how complex the application is. A simple script to pull data from a database and display it in a Web page may all be one procedure, or the behavior of each layer may be placed in separate subroutines. As the system gets more complex, the three layers can be break into separate classes, and as complexity increases, into separate packages.

Choose the most appropriate form of separation for your problem but make sure you do some kind of separation, at least at the subroutine level.

Case study: Hexagonal Architecture

There is a lot of similarity between the presentation and data source layers in that they both are about connection to the outside world. This is the logic behind Alistar Cockburn’s Hexagonal Architecture pattern, which visualizes any system as a core surrounded by interfaces to external system.

Everything external is fundamentally an outside interface, and thus it’s a symmetrical view rather than an asymmetric layering scheme as the three-layer system.