A Whole Context, Layer by Layer: DDD and LVD Together

Patterns are easy to nod along to and hard to place. The real test of a combined DDD and LVD design is a whole context, from the type definitions at the bottom to the protocol adapters on top, with every file in a folder you can defend. The book walks through one for order management. Here is the same exercise in miniature, set in a community library.

The context

A lending context has one obvious aggregate: the loan. A loan has a root, a few inner entities such as the individual items on it, and rules about how long things may be kept. The business speaks of borrowing, renewing, returning and fines, and the code should say the same.

Here is where each part lands.

  1. Schema (L1). Value objects such as a loan period and an item barcode, the entities inside a loan, the loan aggregate root itself, and events such as LoanOpened and ItemReturned. Every name is one a librarian would use. The invariants live here too, for example that a returned item must carry a return date.
  2. Utils (L2). Pure functions: work out a due date from a period and a holiday calendar, work out a fine from the number of days overdue. No database and no clock reads, since the date is passed in. Same inputs, same outputs, so they are trivial to test.
  3. Resources (L3). A loan repository, an event publisher, and an anti-corruption layer for the old catalogue system, which reports availability as numeric codes. The adapter turns those codes into the context's own statuses before anyone else sees them.
  4. Tools (L4). Handlers such as borrow-item and return-item. Each loads the loan through the repository, applies the rules, saves the updated aggregate and publishes one event. Each touches one aggregate.
  5. API (L5) and MCP (L6). Thin adapters that expose the tools, say a route for the front desk, and an MCP server so an assistant can answer "is this book on loan?" without any business logic of its own.

What the walkthrough shows

The book's order management context follows the same arc, with pricing and total calculations in Utils, a payment gateway translator in Resources, and place-order and confirm-order tools above them. Its closing observation is that each layer contributes exactly its designated concern: pure calculation is isolated at L2, outside access and event publication are delegated to L3, and the Tool handler at L4 orchestrates the overall workflow.

One consequence is worth spelling out. The translation between the vendor's model and the domain model is entirely contained in Resources, so Tool handlers never see it. In our library, if the catalogue vendor renumbers its status codes, one adapter file changes. If the library alters the grace period before fines start, the change lands in a Utils function and perhaps a rule in the schema. Neither change should touch the borrow-item handler, because neither concern belongs to it. The same reasoning works for the order management case in the book: a new payment provider means a new translator in Resources, and the workflow above it carries on as before.

That is the quiet strength of the combined design. A change request can be sorted by its kind, and the kind tells you which layer to open. The book's version of the exercise runs to full listings for every layer; this sketch is only meant to show the shape.

The takeaway

A complete context is not a different architecture from the pieces you have already seen. It is the same few decisions, repeated until every file has an owner: words in Schema, calculations in Utils, outside contact in Resources, workflow in Tools, thin adapters above. For the placement rules see mapping DDD onto the layers, for the handler discipline see aggregates and the Tools layer, and for the translator pattern see keeping external systems at arm's length.

What the book covers

  • The book's full order management context with listings for every layer
  • Pricing and total calculations written as pure Utils functions
  • A payment gateway anti-corruption layer inside Resources
  • Place-order and confirm-order tool handlers and the events they publish