Domain Events Across Layers

A parcel locker records that a courier has deposited a parcel. The notifications team wants to text the recipient, billing wants to start the storage clock, and analytics wants a row for its dashboard. None of them should be able to break the locker code. Domain events let one part of a system speak without knowing who is listening, and LVD gives that speech a precise address in each layer.

A contract that lives at the bottom

The book treats domain events as first-class citizens of a combined DDD and LVD design, and it defines their schemas in L1. The reasoning is that an event's shape is a contract between bounded contexts. Change it carelessly and every consumer is affected, so it belongs in the layer that changes least and demands coordination when it does.

A schema also gives you more than a type. Producers and consumers share one definition, the data can be validated at runtime where contexts meet, and the contract is machine-readable, so it can be versioned and documented. For the locker, a ParcelDeposited event with a locker id, a parcel id and a timestamp would sit next to the other Schema types, where every context can import it.

Who emits and who listens

Emission belongs to the Tools layer. A tool handler publishes its event only after the state change it describes has succeeded, so nobody downstream is told about something that did not happen. In the locker, the deposit tool would first record the parcel through its repository and publish ParcelDeposited only once that write had gone through. A failed write then produces no text message and no storage charge.

Consumption belongs to Resources. In the receiving context, an event listener is infrastructure, so it is written as an L3 Resource. It deserialises each message and validates it against that context's own copy of the schema, which is a natural implementation of the published-language pattern. The practical benefit shows up when the upstream team changes its format: the downstream context fails loudly at the Resource layer, and corrupted data never reaches business logic.

A small sketch of the idea for the notifications context:

// resources/depositListener.ts  (L3, notifications context)
import { ParcelDepositedSchema, type ParcelDeposited } from "../schema/parcelEvents";

export function onDeposit(
  raw: unknown,
  handle: (e: ParcelDeposited) => Promise<void>,
) {
  const event = ParcelDepositedSchema.parse(raw); // throws if the shape drifted
  return handle(event);
}

The handler arrives as a parameter, so the Resource never imports the layer above it, and the parse sits at the very edge of the context. Whatever goes wrong is caught there, with a stack trace that points at the contract and not at some business function three calls later.

Event sourcing fits the same layers

Event sourcing stores the full sequence of events instead of the current state. LVD gives each part of that approach a home:

  • The event type definitions are Schema (L1).
  • Rebuilding the current state is a pure fold over the events, which makes it a Utils (L2) function.
  • The client for the append-only event log is a Resource (L3).
  • Handling a command, which means validating it, producing events and persisting them, is a Tool (L4).

For the parcel locker, "is compartment 12 free?" is the result of folding every deposit and collection event for that compartment. Because the fold is pure, you can test it by feeding it a list of events and checking the answer. No database is involved.

The takeaway

Events are not a layer of their own in LVD. They are a contract defined once at the bottom, a side effect of Tools, and an input handled by Resources. Keeping those three roles apart is what stops event-driven systems from turning into a tangle of hidden dependencies. For the wider architectural picture read event-driven architecture with LVD, and for multi-step coordination see aggregates and the Tools layer and transaction boundaries and sagas.

What the book covers

  • Event schemas as contracts between bounded contexts, and why they sit in L1
  • How a tool handler emits an event after a state change succeeds
  • Event listeners in consuming contexts implemented as Resources
  • A layer-by-layer treatment of event sourcing