When each adapter validates business rules independently, the rules can drift: a REST endpoint, a Kafka consumer, and a GraphQL adapter may accept different titles. An always-valid domain model moves those rules into the domain, so every adapter encounters the same construction-time checks. section 2.4.7 section 3.3
A domain object must not be instantiated if it violates a business invariant. Make its constructor private and expose a factory method that validates before creation. On failure, return errors rather than an invalid object. Immutable entities and rich value objects help preserve the guarantees callers rely on after construction. section 3.3 section 1.3 section 3.10
Listing 3.3 Implementation of a factory method to enforce validation in construction
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class Article {
private final ArticleId id;
private final Title title;
private final Content content;
private final AuthorId authorId;
public static Validation<Seq<DomainError>, Article>
validateThenCreate( #A
final ArticleId id,
final Title title,
final Content content,
final AuthorId authorId) {
return Validation.combine( #B
validateNotNull(id, "Article ID"),
validateTitle(title),
validateContent(content),
validateNotNull(authorId, "Author ID")
).ap(Article::new);
} #C
private static Validation<DomainError, Title>
validateTitle(final Title title) {
return title == null || title.value().isBlank()
? Validation.invalid(
new DomainError("Title must not be blank"))
: Validation.valid(title);
}
private static Validation<DomainError, Content>
validateContent(final Content content) {
return content == null || content.value().isBlank()
? Validation.invalid(
new DomainError("Content must not be blank"))
: Validation.valid(content);
}
private static <T> Validation<DomainError, T>
validateNotNull(final T value, final String name) {
return value == null
? Validation.invalid(
new DomainError(name + " must not be null"))
: Validation.valid(value);
}
}
The Article factory combines field checks and invokes the private constructor only when validation succeeds. section 3.3.2
Use Vavr Validation to accumulate independent field errors. For example, a null title and blank content can produce two errors in one result. Convert Validation to Either when chaining dependent operations: look up the author, create the article, then save it. The handler orchestrates this flow; it does not duplicate the entity’s business validation. section 3.3.2 section 1.3
Listing 3.5 Validated creation of an article inside a handler
@ApplicationService
@RequiredArgsConstructor
public class CreateArticleHandler implements CreateArticleUseCase {
private final ArticleRepository articleRepository;
private final AuthorOutputPort authorOutputPort;
@Override
public Either<List<DomainError>, Void>
handle(final CreateArticleCommand command) {
return this.authorOutputPort
.lookupAuthor(command.authorId()) #A
.flatMap(author -> Article.validateThenCreate( #B
command.id(), command.title(), command.content(), author.id())
.mapError(Seq::asJava)
.toEither()) #C
.flatMap(this.articleRepository::save);
}
}
The handler chains author lookup, validated creation, and repository save, propagating failures instead of persisting an invalid article. section 3.3.2
Keep semantic business invariants in the framework-free domain; syntactic input validation belongs at the boundary. A simple factory can live on the entity. For construction involving identity generation or external lookups, a standalone factory is an option. section 3.10
Identity must also fit construction. An application-generated UUID can exist before the entity is created; alternatively, a repository can provide the next identity. Allowing an entity to exist temporarily without an ID is a documented compromise, not the strict always-valid approach. Distinct new and persisted types can make that transition explicit. section 3.7
Test the factory directly without a Spring context or database: verify successful construction and rejection of invalid inputs. For validated entities, use a regular class with a private constructor; the supplied Java approach does not use records for this factory-enforced boundary. section 3.3.2 section 3.3.4
