liveBook · Manning

Data transfer object

As explained in Data-Oriented Programming in Java

A data transfer object is a data-carrying structure used to exchange information between software components without encapsulating domain behavior.

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

How Data-Oriented Programming in Java covers it

How deep it goes
Demonstrates explicit field-by-field translation and defers replacing legacy repositories or entities.
Examples
Uses late-fee-to-invoice conversion, repository saving, and modern-to-legacy serialization as concrete examples.
What it assumes
Assumes familiarity with domain models, persistence entities, repositories, and deterministic data transformation.
What you can do after
Apply it by translating domain data into legacy structures while preserving freedom to evolve the domain model.
How it connects
Connects domain models, legacy entities, ORM machinery, repositories, and data-oriented refactoring.

Also explained in

Compare how 10 books explain data transfer object →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.