CKTIS / Java
DTOs and domain objects: explicit mapping
Separate data sent across a boundary from domain state and behavior. Map five fields explicitly to create independent snapshots that never carry `passwordHash`.
Version: Java SE 21
Syntax
record UserDto(long id, String username, String email, String displayName, boolean active) {}
static UserDto toDto(User user) {
return new UserDto(user.id, user.username, user.email, user.displayName, user.active);
}
static User fromDto(UserDto dto) {
return new User(dto.id(), dto.username(), dto.email(), dto.displayName(), dto.active(), "***");
}DTO versus domain and explicit mapping is about deciding what crosses a boundary. User is a domain object: it holds state and exposes domain operations such as rename. UserDto is a Data Transfer Object (DTO): it defines what is sent across a boundary such as an API response. It has exactly five fields: id, username, email, displayName, and active. passwordHash is omitted altogether, not masked in the DTO.
toDto explicitly selects fields and creates a new record. fromDto reads the same values through record accessors and constructs a new User. The DTO is an independent snapshot, not a live view: renaming the user does not change an existing DTO. A record is only shallowly immutable. Here, primitive values and immutable String objects share no mutable state. If you add mutable components, use defensive copies where needed, including when exposing values.
Mapping works in both directions, but it is not fully reversible: going from DTO to domain and back preserves the five values but cannot recover omitted state. *** is a demonstration placeholder, not a real password hash or a login mechanism. In production, validate incoming data, update existing objects through permitted domain operations, and manage credentials separately.
Examples
public class Main {
static final class User {
private final long id;
private final String username;
private final String email;
private String displayName;
private final boolean active;
private final String passwordHash;
User(long id, String username, String email, String displayName,
boolean active, String passwordHash) {
this.id = id;
this.username = username;
this.email = email;
this.displayName = displayName;
this.active = active;
this.passwordHash = passwordHash;
}
void rename(String name) {
displayName = name;
}
}
record UserDto(long id, String username, String email, String displayName, boolean active) {}
static UserDto toDto(User user) {
return new UserDto(user.id, user.username, user.email, user.displayName, user.active);
}
static User fromDto(UserDto dto) {
return new User(dto.id(), dto.username(), dto.email(), dto.displayName(), dto.active(), "***");
}
public static void main(String[] args) {
User original = new User(42L, "ada", "ada@example.test", "Ada", true, "demo-only");
UserDto snapshot = toDto(original);
original.rename("Ada Lovelace");
User rebuilt = fromDto(snapshot);
System.out.println("original=" + original.displayName);
System.out.println("snapshot=" + snapshot.displayName());
System.out.println("rebuilt=" + rebuilt.displayName);
System.out.println("sameDto=" + snapshot.equals(toDto(rebuilt)));
System.out.println("placeholder=" + "***".equals(rebuilt.passwordHash));
}
}
Result
original=Ada Lovelace snapshot=Ada rebuilt=Ada sameDto=true placeholder=true
Save as `Main.java` and run `java Main.java` with JDK 21. After the rename, the original shows `Ada Lovelace`, while the snapshot and rebuilt object still show `Ada`. `sameDto=true` concerns only the five DTO fields; `placeholder=true` confirms the placeholder was used.
Related lessons
- Java · Lesson 10 · Advanced · Version: 2
Sources
- Record (Java SE 21 & JDK 21)
- Java Language Specification, Java SE 21 Edition — §8.10 Record Classes
- String (Java SE 21 & JDK 21)
- The java Command — Using Source-File Mode to Launch Single-File Source-Code Programs
- Martin Fowler — Data Transfer Object
- OWASP API Security Top 10 — API3:2023 Broken Object Property Level Authorization