275
CHAPTER 6 | Tackle Business Complexity in a Microservice with DDD and CQRS Patterns
public
string
CardHolderName {
get
;
private
set
; }
[DataMember]
public
DateTime CardExpiration {
get
;
private
set
; }
[DataMember]
public
string
CardSecurityNumber {
get
;
private
set
; }
[DataMember]
public
int
CardTypeId {
get
;
private
set
; }
[DataMember]
public
IEnumerable
OrderItems => _orderItems;
public
CreateOrderCommand
()
{
_orderItems =
new
List();
}
public
CreateOrderCommand
(List basketItems,
string
userId,
string
userName,
string
city,
string
street,
string
state,
string
country,
string
zipcode,
string
cardNumber,
string
cardHolderName, DateTime cardExpiration,
string
cardSecurityNumber,
int
cardTypeId) :
this
()
{
_orderItems = basketItems.
ToOrderItemsDTO
().
ToList
();
UserId = userId;
UserName = userName;
City = city;
Street = street;
State = state;
Country = country;
ZipCode = zipcode;
CardNumber = cardNumber;
CardHolderName = cardHolderName;
CardExpiration = cardExpiration;
CardSecurityNumber = cardSecurityNumber;
CardTypeId = cardTypeId;
CardExpiration = cardExpiration;
}
public
class
OrderItemDTO
{
public
int
ProductId {
get
;
set
; }
public
string
ProductName {
get
;
set
; }
public
decimal
UnitPrice {
get
;
set
; }
public
decimal
Discount {
get
;
set
; }
public
int
Units {
get
;
set
; }
public
string
PictureUrl {
get
;
set
; }
}
}
Basically, the command class contains all the data you need for performing a business transaction by
using the domain model objects. Thus, commands are simply data structures that contain read-only
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ş: