about ddd domain model and cqrs



I have a project about Product Management System. The operator can add new product with it .


As a product , some info (property) is optional and some is mandatory. How to design command object for product creation, if contain the optional info ?

In my opinion, the create command should only contain mandatory info ,and the optional info can be updated after creation.

any suggestion?

sample code (may be):

class CreateProductCommand

private String name; //mandatory
private String catId; //mandatory

public CreateProductCommand(String name,String catId)

some get method…


class Product
private String uuid; //mandatory
private String name; //mandatory
private String catId; //mandatory
private String description; //optional

public Product(String uuid,String name,String catId)
apply(new ProductCreatedEvent(…));



In my experience/opinion it is best to issue a single create command containing both mandatory and optional data.

A common use case is user registration. Here, the command to create the user usually contains the user id and user profile. This user profile might be rudimentary with little useful data if the signup process is basic but the advantage is that a subsequent command to edit the user profile also contains a user profile object. This way, both commands share the same language which usually makes life easier.

In general, it should be clear what the intent of a command is. By splitting up your command it is harder for your aggregate to know what’s going when it needs to handle the second command.


thanks René de Waele

if the command contains all datas, handler will use ALL data to create new ENTITY(product,above sample), and entity should have a constructor with ALL parameters.

according to the business, some optional data can be ignored when entity created, so that confuses me.

should I add a constructor of entity class with ALL OPTIONAL parameters?

Just shooting from the hip here, but couldn’t you have a constructor as follows:

public ProductEntity(String productId, ProductData productData) {
this.productId = productId;

The setProductData method can also be called when the product data is updated at a later time.

You can annotate ProductData with @Embeddable to prevent copying over all it’s fields to the ProductEntity.


got it

thanks again

Joshua Yan