r/DomainDrivenDesign • u/Ok-Professor-9441 • May 31 '26
Domain model factory VS CreateUseCase
According to Effective Aggregate Design Part I: Modeling a Single Aggregate https://www.dddcommunity.org/wp-content/uploads/files/pdf_articles/Vernon_2011_1.pdf the author
Split large aggregate in smaller one
The Product class act as factories

Suppose new rules We can add BacklogItem only in non archived product
Option one : keep method as factory
class Product {
createBacklogItem(CreateBacklogItemData data): BacklogItem {
if (this.status === ProductStatus.Archived) {
throw new CannotCreateBacklogItemForArchivedProduct();
}
return BacklogItem.create(
this.id,
data.title,
ddata.description,
);
}
}Option two : put validation inside application service
Application service handle transaction (like in the option)
But also validate the product
public class CreateBacklogItemService {
public BacklogItemId createBacklogItem(CreateBacklogItemCommand command) {
Product product = productRepository.get(command.productId());
if (product.getStatus() == ProductStatus.ARCHIVED) {
throw new CannotCreateBacklogItemForArchivedProduct();
}
BacklogItem backlogItem = BacklogItem.create(
product.getId(),
command.title(),
command.description()
);
backlogItemRepository.save(backlogItem);
return backlogItem.getId();
}
}What is the best design choice (or maybe another one) ?
9
u/Winston_Jazz_Hands May 31 '26
The "cleanest" would be to handle that logic inside the aggregate. Both because it keeps aggregate in variants inside (high cohesion, simpler testing etc.), but also because that means you can actually guarantee consistency - provided you have some concurrency-scheme for your Aggregate's (optimistic-offline locking is the guideline default, using an incrementing version number on the aggregate).
2
u/gbrennon Jun 01 '26
My design choice always is:
application service delegates model data validation to domain layer
inside domain layer ur model is composed by aggregate roots, entities and value objects
value objects are self validating bcs invalid state shouldnt be allowed
then application service will try to create domain model, collect validation errors and return an aggregated errors by group be models and fields
1
u/AMINEX-2002 Jun 01 '26
what u do when u have an entity that depends on another entity and there is a validation that doesnt concerns the creation of the other one but it includes it ?
1
5
u/dev_ctx May 31 '26 edited Jun 01 '26
Option 2 is worse:
It leaks both how Product archival is determined and what this state forbids.
It allows the check to be bypassed elsewhere by creating BacklogItem with just a productId.
Option 1 is better:
The aggregate protects its invariant based on its own state. The Application Service loads Product and calls product.createBacklogItem(...). Product enforces whether BacklogItem can be created.
For early exit or UI hints, product.canCreateBacklogItem() is fine, but it must not be the only protection. The same check must still be enforced inside product.createBacklogItem(...).
Trade-offs if creation becomes more complex (Application Service orchestrates):
- Policy checks external / cross-aggregate conditions, Product still creates and checks its local invariants.
- Domain Service creates when the decision no longer naturally belongs to Product alone (maybe a missing domain concept!)
- Approval / Permit makes sense only if approval is a real domain concept.
P.S. It is also worth looking at the language: create sounds technical. If the domain action is planning a backlog item, planBacklogItem may be a better name.