Why do value objects need to be immutable in DDD?
Alright, while it is not mandatory to make VO as immutable (even DDD book doesn’t say they have to be immutable), the main idea, in DDD, to make it a VO seems to be not to deal with life cycle complexities like that of an Entity. Look here for more details. Value objects need to be immutable.
Why do value objects need to be immutable?
Now, this is the fundamental concept of Value Objects. Their value is determined by their state. An Entity, however, is not determined by its state. A Customer can change their last name, or their address and still be the same Customer. This is why Value Objects should be immutable.
Which is an example of a value object?
Because they are defined by their attributes, value objects are treated as immutable. A good example of a value object is money. It doesn’t matter that you can’t distinguish between the same five one dollar bills in your pocket. You don’t care about the currency’s identity -only about its value and what it represents.
How are value objects implemented in eshoponcontainers?
For example, in the Ordering domain model at eShopOnContainers, as part of the Order entity, the Address value object is implemented as an owned entity type within the owner entity, which is the Order entity. Address is a type with no identity property defined in the domain model.
What are the advantages and disadvantages of immutable objects?
Deeply-immutable objects have the advantage that deep-cloning something simply requires copying a reference. They have the disadvantage that making even a small change to a deeply-nested object requires constructing a new instance of every object within which it is nested.
When to call asimmutable on a mutable object?
When calling AsImmutable on a mutable object, before returning a new immutable object, check it would match the cached reference. If so, return the cached reference (abandoning the new immutable object). Otherwise update the cached reference to hold the new object and return that.
How are immutable types similar to mutable types?
Immutable types with mutable equivalents would sport an AsMutable method, which would construct a mutable instance whose properties matched those of the original.