Loading an existing aggregate is not the same as creating one. Reconstitution restores persisted state, including its status, without rerunning creation-time validation, raising events, or failing. This avoids rejudging past decisions against changed rules; handle legacy conflicts through migration, grandfathering, or revalidation on a later modification. section 9.2.3
Listing 9.5 Article: The aggregate root, with its two entry points
@Accessors(fluent = true)
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@AllArgsConstructor(access = AccessLevel.PRIVATE) #A
public class Article {
@Getter
@EqualsAndHashCode.Include
private final ArticleId id; #B
@Getter
private final Title title;
@Getter
private final Content content;
@Getter
private final AuthorId authorId;
@Getter
private ArticleStatus status; #C
// No getter to avoid direct manipulation of the collection.
private final List<DomainEvent> domainEvents; #D
public static Either<Error, Article>
validateThenCreate(final ArticleId id, final Title title,
final Content content, final AuthorId authorId) {
final List<ValidationError> errors =
new ArrayList<>(); #E
Validations.mandatory(id, "id")
.peekLeft(errors::add);
Validations.mandatory(title, "title")
.peekLeft(errors::add);
Validations.mandatory(content, "content")
.peekLeft(errors::add);
Validations.mandatory(authorId, "authorId")
.peekLeft(errors::add);
return errors.isEmpty()
? Either.right(
new Article(id, title, content, authorId,
ArticleStatus.DRAFT, new ArrayList<>())) #F
: Either.left(new Error.ValidationErrors(List.copyOf(errors)));
}
public static Article reconstitute(final ArticleId id,
final Title title, final Content content,
final AuthorId authorId,
final ArticleStatus status) { #G
return new Article(id, title, content, authorId, status,
new ArrayList<>());
}
// publish(...) and popDomainEvents() follow in listing 9.6
}
The Article aggregate has separate entry points for validated creation and reconstitution. section 9.2.3
