The question refers to a situation in which you have to update/delete a valueobject.
Consider this situation.
You have an Order and you have Products and Products have ProductItems. Where ProductItem is a ValueObject. If you would have to update the list of productItems in Product (let's say update the quantity), how would you do it? How will you identify the exact ProductItem that requires the update ?
This is from the perspective or an OrderingContext, there should be a backing context where you have ProductItem stock and ProductItem pricing (InventoryContext), context where a ProductItem is more then just a ValueObject, but since we are under the OrderingContext, how would you update a ProductItem if you don't have a way to identify it?
You identify the
Value object
in a list by whatever means you need, there is not rule regarding that.For example, in this case, one way to identify it is to use it's numeric index from the list. If you need something more stable (i.e. that works also in case of items reordering) you can have GUIDs instead of numeric indexes, in languages that support this. For example,
PHP
lets you usestrings
for array access andGUIDs
can be converted tostrings
before use in an array lookup.The important rule to remember about
Value objects
: they should be immutable, that is, if you need to update one then you replace it with another instance.