An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
November 6, 2022 01:27 pm GMT
Original Link: https://dev.to/leriaetnasta/dto-as-a-record-41gi
DTO as a record
With DTOs we're dealing with immutable data more often than not, which means that once an object is created, we cannot change its content.
To accomplish this in Java, we create a class with private, final fields and public accessors.
Take a SpaceShipDTO class for example
final class SpaceShipDTO { final String name; final String description; final int weapons; public SpaceShip(String name, String description, int weapons) { this.name = name; this.description = description; this.weapons = weapons; } String getName() { return name; } double getDescription() { return description; } double getWeapons() { return weapons; }}
We can easily replace this with a record
record SpaceShipDTO(String name, String description, int weapons){ }
This record automatically creates 3 private final fields, public accessor methods, a constructor and implementations of equals(), hashCode(), and toString() methods, all generated by the Java compiler.
Original Link: https://dev.to/leriaetnasta/dto-as-a-record-41gi
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To