SwiftPumpkin

12 of 20 · 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

Swift example: leaked Core Data type vs. mapped domain boundary

Passing an NSManagedObject into a ViewModel ties UI code to Core Data's thread-confined context.

Before vs. after: map storage records at the data boundary

Domain structs are plain Sendable values; Core Data or SQLite stays isolated inside the data adapter.

// ❌ Before: Core Data leaks into presentation (non-Sendable, crashes off-queue).
final class LeakyNotesVM {
  var entities: [NoteManagedObject] = []  // tied to NSManagedObjectContext!
}

// ✅ After: Domain defines plain Sendable types + ports; Data maps at the boundary.
struct Note: Sendable, Identifiable {
  let id: UUID
  let body: String
}

protocol NoteRepository: Sendable {
  func fetchNotes() async throws -> [Note]
}

final class CoreDataNoteRepository: NoteRepository {
  private let context: NSManagedObjectContext
  init(context: NSManagedObjectContext) { self.context = context }

  func fetchNotes() async throws -> [Note] {
    try await context.perform {
      // Map NSManagedObject -> plain Sendable Note before crossing the boundary.
      try self.context.fetch(NoteManagedObject.fetchRequest()).map {
        Note(id: $0.id, body: $0.body)
      }
    }
  }
}

For large tables, paginate or window queries at the repository boundary rather than eagerly mapping thousands of rows.

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

Done with Decoupled Layers? Mark it complete. Nailing its cards builds this pumpkin, row by row.