SwiftPumpkin

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

Depends onImplements portsUI layerScreensData layerDisk, networkDomainModels, rules, ports
calls or tellsreports backEvery arrow points at the domain. The domain imports nothing but Foundation.
  • PumpkinDomain defines entities and ports.
  • PumpkinStorage and PumpkinRPC implement them.
  • The app only talks to ports.
PartOwnsLooks like
DomainEntities, rules, protocols (ports)PumpkinDomain
DataAdapters implementing portsPumpkinStorage, PumpkinRPC
UIScreens talking to ports onlyApp 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 frameworkDomain package plus adapters.
Several teams sharing models and rulesA domain module everyone depends on.
PrototypesFolders. 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.

30-second answer

Explain Decoupled Layers in 30 seconds.

System designAsked directly

State the dependency rule. How does a use case call out to a presenter without breaking it?

System designAsked indirectly

The domain layer passes Core Data managed objects straight up to views. What's the risk, and the fix?

System designAsked indirectly

The same business rules must power iPhone, iPad and Mac apps. How do you layer the code?

System designAsked indirectly

Rules like whether a user can comment are copied across view controllers and a global helper. Where should they live?

Read it in the app's source