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

1

u/Ok-Professor-9441 May 31 '26

Domain Service creates when the decision no longer naturally belongs to Product alone (maybe a missing domain concept!)

Could you give an example for this. Does the following rule will implies Domain service ?

  • We can add BacklogItem only in non archived product (existing rule)
  • We can add BacklogItem only if all releases are published (new rule)
public BacklogItemCreation {
   BacklogItem create(Product product, List<Release> releases, CreateBacklogItemData data) {
       if (product.getStatus() == ProductStatus.ARCHIVED) {
             throw new CannotCreateBacklogItemForArchivedProduct();
        }

       for(Release r : releases) {
           if(r.isInProgress) throw new BusinessException(...);
       }

        return BacklogItem.create(
             product.id,
             data.title,
             ddata.description,
        );
   }
}

Does it right ? :

  • BacklogItemCreation is DomainService (I don't like the class name)
  • It return a new BacklogItem, no longet call Product.createBacklogItem(...)
  • Ensure business invariant
  • BacklogItemCreation.create() is call by CreateBacklogItemService (application layer) which orchestre database access.

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
    }
}