Command design

Hi
I am struggling a bit with the design of my command (and events).
What are best practices or recommended patterns for designing commands?
Assume there is an aggregate Operator that handles a commend to add a postal address:

Option 1: POJO-style command:

@Value
@Builder
class AddPostalAddressCommand {

@TargetAggregateIdentifier
OperatorId operatorId;
String street;
String streetNumber;
String postalCode;
String city;
String country;
}

Option 2: Use value objects from the model also in the commands (and events):

@Value
@Builder
class AddPostalAddressCommand {

@TargetAggregateIdentifier
OperatorId operatorId;
PostalAddress postalAddress; // value object from my model
}

The value object approach feels more DDD-like, but on the other hand side this leads to value objects “leaking” out of my model. I’ll end up with a lot of value objects in my API (commands+events).

Any suggestions/opinions on this?

Thanks
Klaus

Hi,

  1. I like to keep messages flat and simple (without complex multilevel structure). This enables maintainability and your API (messages) can evolve more independent (decoupled from deep domain components/aggregates)
  2. I can understand that sometimes some structure is adequate on the message level (readability). In this case, you can introduce value objects (immutable objects) in the structure. These immutable objects do not have to be the same value object you have on the deep domain model level (for example value objects that belong to Aggregate), in this case, you will have to map them in your aggregate and this requires a bit of more code but you can still evolve your aggregate and API (messages) independently and prevent leaking.

Best,
Ivan

1 Like