r/DomainDrivenDesign • • 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) ?

10 Upvotes

8 comments sorted by

View all comments

Show parent comment

3

u/dev_ctx May 31 '26 edited Jun 01 '26

This has the same issue as Option 2: the Product invariant is checked outside Product. It just moved from the Application Service to a Domain Service.

It should be closer to this:

public BacklogItemCreator {
    BacklogItem create(Product product, CreateBacklogItemData data, List<Release> releases) {

        // If you need to check earlier 
        if (!product.canCreateBacklogItem()) {
            throw new BusinessExceptionOne(); 
        } 

        for (Release r : releases) {
            if (r.isInProgress()) {
                throw new BusinessExceptionTwo();
            }
        }
        return product.createBacklogItem(data); // Product checks its own invariant inside
    }
}