276
CHAPTER 6 | Tackle Business Complexity in a Microservice with DDD and CQRS Patterns
data, and no behavior. The command’s n
ame indicates its purpose. In many languages like C#,
commands are represented as classes, but they are not true classes in the real object-oriented sense.
As an additional characteristic, commands are immutable, because the expected usage is that they are
processed directly by the domain model. They do not need to change during their projected lifetime.
In a C# class, immutability can be achieved by not having any setters or other methods that change
the internal state.
Keep in mind that if you intend or expect commands to go through a serializing/deserializing process,
the properties must have a private setter, and the
[DataMember]
(or
[JsonProperty]
) attribute.
Otherwise, the deserializer won’t be able to reconstruct the object at the
destination with the required
values. You can also use truly read-only properties if the class has a constructor with parameters for all
properties, with the usual camelCase naming convention, and annotate the constructor as
[JsonConstructor]
. However, this option requires more code.
For example, the command class for creating an order is probably similar in terms of data to the order
you want to create, but you probably do not need the same attributes. For instance,
CreateOrderCommand
does not have an order ID, because the order has not been created yet.
Many command classes can be simple, requiring only a few fields about some state that needs to be
changed. That would be the case if you are just changing the status of an order from “in process” to
“paid” or “shipped” by using a command similar to the following:
[DataContract]
Dostları ilə paylaş: