Mutable or immutable domain classes? #49
Unanswered
marciovmartins
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Context
I have an order aggregate with line items, and those line items have specific configurations (for digital products it is an url, for physical products is the sku and other stuff).
There is this use case to change the configuration of an product.
Usually my domain classes are immutable and do calls like that:
order.updateLineItemConfiguration(lineItemId, newConfiguration)
This returns a value object
Configuration
where I can send to the repository to persist this properly:orderRepository.update(lineItemId, configuration)
And reload the
order
domain class from the repository again.Would you make the
configuration
property not final and change it in theorder
domain class?What I try to avoid with mutable classes is to have some generic method like
orderRepository.update(order)
and this method have to look everything that could be changed in the order to update in the repository.I'm asking this question because my tests starts to become more complex to write, specially the
FakeOrderRepository
, where I have to iterate over the line item list, find the proper one, create a new line item with the new configuration and finally create the aggregate order replacing the old line item by the new one.This effort looks wrong and unnecessary, and since it is several steps it is easier to introduce an error and more difficult to implement. Where is the opposite of this kind of strategy where should have a easy way to implement minimizing the possibility of error.
Any idea?
Beta Was this translation helpful? Give feedback.
All reactions