14 of 22 · System architecture
Decoupled Layers
Decoupled layers organize code so dependencies point inward: the UI and data layers depend on the domain, and the domain (models, rules and protocols, or ports) depends on nothing.
How it's wired
- PumpkinDomain defines entities and ports.
- PumpkinStorage and PumpkinRPC implement them.
- The app only talks to ports.
| Part | Owns | Looks like |
|---|---|---|
| Domain | Entities, rules, protocols (ports) | PumpkinDomain |
| Data | Adapters implementing ports | PumpkinStorage, PumpkinRPC |
| UI | Screens talking to ports only | App target |
How to tell the layers are real
- The swap test: could you replace disk storage with CloudKit without touching a view? If not, a layer leaked.
- Map at the boundary. The data layer converts DTOs and managed objects into domain structs. A Core Data type inside a ViewModel is a leak.
- Enforce with modules, not folders. A Swift package per layer makes an illegal import fail to compile.
- Don't over-layer. Every boundary costs mapping code. A two-screen app needs folders, not five packages.
When I'd layer formally
| Apps expected to outlive their backend or UI framework | Domain package plus adapters. |
|---|---|
| Several teams sharing models and rules | A domain module everyone depends on. |
| Prototypes | Folders. Formal layers come later. |
Expert notes
- Layers aren't about neatness. They decide what you can change without fear.
Check yourself
Answer out loud before you reveal. Recall is what makes it stick.
Explain Decoupled Layers in 30 seconds.
- Decoupled layers point every dependency inward: the UI and data layers depend on the domain, and the domain (models, rules and protocols) depends on nothing.
- Adapters convert DTOs and database objects into domain types at the boundary.
- My test: could I swap disk storage for CloudKit without touching a view?
- The cost is mapping code at every boundary, so I don't formalize layers for a two-screen app.
State the dependency rule. How does a use case call out to a presenter without breaking it?
- Source dependencies point only inward.
- Inner layers never name anything in outer ones, including framework types.
- When control must flow outward, the use case calls a protocol it defines, like
protocol OrderOutput { func present(_ orders: [Order]) }, and the outer presenter implements it. - Control flows out while the source dependency still points in.
- That's dependency inversion.
The domain layer passes Core Data managed objects straight up to views. What's the risk, and the fix?
- Map them to plain structs at the boundary.
- Passing
NSManagedObjectupward couples every layer to Core Data, so views inherit its threading rules, faults and schema changes. - Data crossing a boundary should be simple values shaped for the inner layer.
- The mapping code is boring, but it's what lets you change persistence later without touching views.
The same business rules must power iPhone, iPad and Mac apps. How do you layer the code?
- Keep business logic in plain objects with no UIKit or AppKit import, like VIPER Interactors or use-case types, and share them in a module.
- Give each platform its own presenters and views, since layouts differ.
- Put persistence behind a data manager, so the store can change too.
- Reuse across iPhone, iPad and Mac is exactly what this layering buys you.
Rules like whether a user can comment are copied across view controllers and a global helper. Where should they live?
- Put it in a model controller.
- Models should stay plain data, and a singleton brings shared mutable state.
- A model controller is a viewless object that keeps one model private and exposes purpose-built APIs, like
var canComment: Bool. - Each rule gets one testable code path, and views observe the controller instead of the raw model.
- The cost is a new API per use case.