2. @joachimvda@eliwan_be
What is a tranfer object
• Class with only getters and
setters
• Project Lombok recommended @Data
Public class PersonTo {
private String firstName;
private String lastName;
private String gender;
private String birthday;
}
3. @joachimvda@eliwan_be
Why use transfer objects
• Decouple API and domain
model
• Manage application boundaries
(e.g. transactions)
@Data
@Entity
Public class Person {
@Id
Private Long id;
@Basic
private String firstName;
@Basic
private String lastName;
@Basic
private Gender gender;
@Basic
private LocalDate birthday;
}
5. @joachimvda@eliwan_be
Not always simple...
• Null checks
• Domain lookup
• Type conversion
• Still boilerplate !
Gender gender;
if (StringUtils.
isBlank(to.getGender)) {
Gender = null;
} else {
Gender = Gender.valueOf(
to.getGender());
}
res.setGender(gender);
6. @joachimvda@eliwan_be
In the real world...
• Project for Flemish Government, REST API
• almost 10% of the code was custom conversion between TO and domain
objects
• Inconsistent conversion behaviour
• Stupid work, easy to make mistakes
• Testing
15. @joachimvda@eliwan_be
ORM link, object finder
Automatically build new / linked object
•Find in Hibernate maybe
•Always consistent
Use ReadOnlyDomain to either update links only or linked object contents as well
16. @joachimvda@eliwan_be
Inheritance support
Declare child types to convert to correct object
Normal annotations on child classes (correct domain class)
@DomainClass(value = "pkg.ObjectDomain")
@DomainClassDelegate(delegates = {
SomeChildTo.class, OtherChildTo.class
})
public class ObjectTo {
17. @joachimvda@eliwan_be
Convert interceptor
Set method to call on each convert invocation
Validations
Side effects
<T> T convert(
Object source,
T target,
boolean isTargetTo,
ConvertSourceTarget next,
String... tags);