Which way dependencies point: layers, the MVC family, ports and adapters
Examine dependency direction across layered, hexagonal, onion, and clean architectures, positioning MVC-family presentation patterns in the outer adapter ring.
Contentsand lesson path
On this page
- The Tyranny of the Database-Centric Layer Cake
- Comparing Concentric Architectural Styles
- Positioning the MVC Family in the Outer Ring
- Evaluating the Architectural Cost of Indirection
Lessons of Clean & Hexagonal Architecture
- Which way dependencies point: layers, the MVC family, ports and adapters Concept · 25 min
- Driving ports, driven ports and the composition root Concept · 20 min
- Moving a layered service to hexagonal Lab · 30 min
- Testing the core with in-memory adapters Lab · 20 min
- When a plain layered design is the better choice Concept · 10 min
- Dependency rule check Knowledge check · 6 questions
What you will learn
- Compare layered, hexagonal, onion and clean architecture by dependency direction, place MVC-family presentation patterns (MVC, MVP, MVVM) within them, and judge when the extra indirection is not worth its cost.
Prerequisites: Software Design Principles
01. The Tyranny of the Database-Centric Layer Cake
For decades, enterprise software architecture was dominated by the classic three-tier layered model: Presentation Layer at the top, Business Logic Layer in the middle, and Data Access Layer at the foundation. While this model provided an initial semblance of organization, it contained a fatal architectural flaw: all source code dependencies pointed downward toward the database.
In a traditional three-tier stack, the business layer directly imports database drivers, ORM entities, and SQL connection pools. Consequently, business logic cannot compile, execute, or be tested in isolation from the database schema. If the database schema changes, business entities must change. If the database driver is upgraded, the core business engine must be recompiled. The most precious, enduring asset of any enterprise—its domain rules and business policies—is held hostage by volatile infrastructure plumbing.
Both Alistair Cockburn (Hexagonal Architecture / Ports and Adapters) and Robert C. Martin (Clean Architecture) recognized that this dependency direction is fundamentally inverted. Infrastructure should depend on business logic, never the reverse.
02. Comparing Concentric Architectural Styles
Several prominent architectural paradigms share this fundamental inversion of control, differing primarily in terminology and geometric metaphor:
- Hexagonal Architecture (Ports and Adapters, 2005): Formulated by Alistair Cockburn. It visualizes the application core as a hexagon surrounded by ports (interfaces) and adapters (implementations). The shape has no magical mathematical significance; a hexagon was chosen simply to provide multiple sides to visually emphasize that an application interacts with many distinct external actors (HTTP clients, message queues, databases, cache servers) uniformly.
- Onion Architecture (Jeffrey Palermo, 2008): Organizes code into concentric rings with Domain Model at the absolute center, surrounded by Domain Services, Application Services, and external UI/Infrastructure at the outermost ring. It explicitly separates pure domain rules from use case orchestration.
- The Clean Architecture (Robert C. Martin, 2012): Synthesizes Hexagonal, Onion, and Screaming Architecture into four concentric rings: Enterprise Business Rules (Entities), Application Business Rules (Use Cases), Interface Adapters (Controllers, Gateways, Presenters), and Frameworks & Drivers (Web, DB, UI, Devices).
In all three architectures, the central invariant is identical: the domain core is pure, framework-free, and unaware of external delivery mechanisms.
03. Positioning the MVC Family in the Outer Ring
A recurring point of confusion for web developers transitioning to Clean Architecture is where the classic MVC (Model-View-Controller), MVP (Model-View-Presenter), and MVVM (Model-View-ViewModel) patterns belong. Developers often ask: "Is our MVC framework our architecture?"
The answer is a decisive no. The MVC family consists of presentation patterns, not system architectures. Their sole responsibility is orchestrating user interface interactions, translating incoming HTTP requests or UI gestures into domain invocations, and formatting domain responses into view templates or JSON payloads.
In Clean and Hexagonal Architecture, the entire MVC apparatus lives strictly in the Outer Ring (Interface Adapters):
- Controllers: Act as Driving Adapters. They accept HTTP requests, deserialize transport payloads into domain command arguments, invoke the appropriate application use case port, and pass the resulting domain output to a presenter.
- Presenters / ViewModels: Format raw domain responses into UI-friendly view models (formatting currencies, localized date strings, and display flags) without polluting domain entities with presentation annotations.
- Views: Pure rendering templates or client-side components that render view models onto screen displays.
By confining MVC frameworks to the outermost adapter boundary, changing from a server-rendered web app to a GraphQL API or a CLI tool requires authoring a new controller adapter without touching a single line of domain or use-case code.
Crossing Boundary Seams with Simple Data Structures When data crosses from the use-case layer out to a presenter or controller, it must cross as simple, isolated data structures (such as plain Data Transfer Objects, primitive tuples, or immutable read-only records). Under no circumstances should domain entities cross the boundary into view templates. If an active domain entity is passed directly to an MVC view, the view layer can accidentally invoke entity mutating methods or trigger lazy-loaded database queries, violating encapsulation and triggering catastrophic N+1 query loops.
Screaming Architecture: Architecture as Domain Intent
Robert C. Martin coined the term Screaming Architecture to describe how directory layouts should reflect business intent. When an architect inspects the top-level folder structure of a codebase, it should not scream controllers/, models/, and views/—which merely screams that the system was built using Ruby on Rails or Django. Instead, top-level directories should scream the business capabilities: loan-origination/, customer-billing/, and underwriting/. Technical frameworks are incidental delivery details.
04. Evaluating the Architectural Cost of Indirection
While the Dependency Rule confers extraordinary modularity and testability, it extracts a significant development cost. Adopting Hexagonal Architecture requires creating:
- Dedicated Port interfaces for every external collaborator.
- Independent adapter classes wrapping database queries, network clients, and message producers.
- Explicit translation mappers that convert database rows into domain entities, and domain entities into API view models.
- Dependency injection wiring at the application Composition Root.
For an enterprise system with intricate business rules, long lifecycle expectations, and multiple evolving integration partners, this indirection pays enormous dividends. It enables developers to test use cases entirely in memory in sub-second test suites and swap infrastructure providers without risking regressions.
However, for a straightforward CRUD application, internal admin panel, or throwaway prototype where business rules merely validate input fields and store rows directly, Hexagonal Architecture represents severe over-engineering. In such systems, the mapping code between layers exceeds the volume of actual business logic, creating friction with zero realized benefits.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
6 questions · about 5 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.