214
CHAPTER 6 | Tackle Business Complexity in a Microservice with DDD and CQRS Patterns
Figure 7-13. Address value object within the Order aggregate
As shown in Figure 7-13, an entity is usually composed of multiple attributes. For example, the
Order
entity can be modeled as an entity with an identity and composed internally of a set of attributes such
as OrderId, OrderDate, OrderItems, etc.
But the address, which is simply a complex-value composed of
country/region, street, city, etc., and has no identity in this domain, must be modeled and
treated as a
value object.
Important characteristics of value objects
There are two main characteristics for value objects:
•
They have no identity.
•
They are immutable.
The first characteristic was already discussed. Immutability is an important requirement. The values of
a value object must be immutable once the object is created. Therefore, when the object is
215
CHAPTER 6 | Tackle Business Complexity in a Microservice with DDD and CQRS Patterns
constructed, you must provide the required values, but you must not allow them to change during the
object’s lifetime.
Value objects allow you to perform certain tricks
for performance, thanks to their immutable nature.
This is especially true in systems where there may be thousands of value object instances, many of
which have the same values. Their immutable nature allows them to be reused; they can be
interchangeable objects, since their values are the same and they have no identity. This type of
optimization can sometimes make a difference between software that runs
slowly and software with
good performance. Of course, all these cases depend on the application environment and deployment
context.
Dostları ilə paylaş: