When persistence and surrounding legacy code already speak an Invoice Entity, treat that structure as a data transfer object at the boundary. Keep domain decisions in your own model, then deterministically map its data into the legacy shape. section 6.4.2
A DTO-based boundary lets you:
- Reuse existing persistence and ORM machinery without making the legacy entity your domain model. section 6.4.2
- Make translation explicit by assigning identifiers, customer data, line items, statuses, dates, and audit information. section 6.4.2
- Grow a data-oriented design incrementally: work in the modern model, then serialize it back into the legacy world. section 9.3.5
Mapping a late fee into an Invoice DTO
A late-fee model can be converted into an Invoice by selecting a persisted or temporary identifier, copying customer and date data, creating a late-fee line item, and setting its status and type. The repository then saves the converted structure, while any additional legacy records are handled separately. section 6.4.2
Listing 6.57 Deterministically mapping between worlds
static Invoice toInvoice(LateFee<? extends Lifecycle> latefee) { #A
Invoice invoice = new Invoice();
invoice.setInvoiceId(switch (latefee.state()) { #B
case Billed(var id) -> id.value();
default -> Invoice.tempId(); #C
});
invoice.setCustomerId(latefee.customer().id().value());
invoice.setLineItems(List.of(new LineItem(
null,
"Late Fee",
latefee.total().value(),
Currency.getInstance("USD")
)));
invoice.setStatus(InvoiceStatus.OPEN); #D
invoice.setInvoiceDate(latefee.invoiceDate()); #D
invoice.setDueDate(latefee.dueDate()); #D
invoice.setInvoiceType(InvoiceType.LATEFEE); #D
invoice.setAuditInfo(new AuditInfo(
null,
latefee.includedInFee().stream().map(PastDue::invoice).toList(),
switch(latefee.state()) {
case Rejected(var why) -> why.value();
case InReview(String approvalId) -> approvalId;
default -> null;
}
));
return invoice;
}Inspect the deterministic mapping that turns a domain late fee into the legacy Invoice data shape. section 6.4.2









