with one click
dto-mappers
// DTO and mapper patterns for Xeres using Java records, canonical constructors with validation, and static mapper utility classes.
// DTO and mapper patterns for Xeres using Java records, canonical constructors with validation, and static mapper utility classes.
ArchUnit architecture rules enforced in Xeres including common module rules (logging, utility classes), app module rules (no field injection, RsService naming), and UI module rules (WindowController naming).
Cryptography patterns for Xeres including PGP operations, key generation, and hash functions with best practices.
Flyway SQL migration patterns for Xeres including naming conventions, H2 database patterns, enum types, foreign keys, and best practices.
Gradle build configuration for Xeres including build commands, version management, module structure, and key plugins.
Code style, naming conventions, license headers, and patterns for Xeres Java project. Covers Allman braces, utility classes, package structure, and field injection rules.
JavaFX patterns for Xeres including controller structure with FXML views, WindowController lifecycle, WindowManager usage, and JavaFX-Spring integration.
| name | dto-mappers |
| description | DTO and mapper patterns for Xeres using Java records, canonical constructors with validation, and static mapper utility classes. |
Use Java records for immutable DTOs:
public record ProfileDTO(
long id,
@NotNull @Size String name,
String pgpIdentifier,
Instant created,
byte[] pgpFingerprint,
byte[] pgpPublicKeyData,
boolean accepted,
Trust trust,
@JsonInclude(NON_EMPTY) List<LocationDTO> locations
)
{
public ProfileDTO
{
if (locations == null) locations = new ArrayList<>();
}
}
public record ProfileDTO(...)
{
public ProfileDTO
{
Objects.requireNonNull(name, "Name must not be null");
if (locations == null)
{
locations = new ArrayList<>();
}
locations = List.copyOf(locations); // Make immutable
}
}
Static utility class with mapping methods:
public final class ProfileMapper
{
private ProfileMapper()
{
throw new UnsupportedOperationException("Utility class");
}
public static ProfileDTO toDTO(Profile profile)
{
if (profile == null)
{
return null;
}
return new ProfileDTO(
profile.getId(),
profile.getName(),
// ... other fields
);
}
public static Profile toEntity(ProfileDTO dto)
{
if (dto == null)
{
return null;
}
var profile = new Profile();
profile.setId(dto.id());
profile.setName(dto.name());
// ... other fields
return profile;
}
}
// Entity to DTO
ProfileDTO dto = ProfileMapper.toDTO(profile);
// DTO to Entity
Profile profile = ProfileMapper.toEntity(dto);
// List mapping
List<ProfileDTO> dtos = profiles.stream()
.map(ProfileMapper::toDTO)
.toList();
@JsonInclude(NON_EMPTY) // Don't serialize null or empty collections
List<LocationDTO> locations
Use Bean Validation on DTO fields:
@NotNull
@Size(min = 1, max = 255)
String name
@Email
String email
@Min(0)
@Max(100)
int percentage
Always handle null collections in constructor:
public ProfileDTO
{
if (locations == null)
{
locations = new ArrayList<>();
}
}