I'm stuck on finding the proper way to refer to entities located inside an aggregate root, when we only got their identities coming from URL parameters. I asked a previous question which ended up focused on value objects, so I'm starting with another example here.
Let's say we want to modify an OrderLine
inside an Order
:
- The user goes to a page where he can see the Order summary along with all its Order Lines.
- The user clicks on the edit button next to an Order Line.
- He gets directed to
edit-order-line?orderId=x&orderLineId=y
Now if I need to update the quantity in the OrderLine, I can do:
Order order = orderRepository.find(orderId);
order.updateQuantity(orderLineId, 2);
However, I don't feel very comfortable with the idea of leaving the responsibility to the Order to retrieve parts of itself by Id. My view on the subject is that within the domain, we should just talk with objects, and never with Ids. Ids are not part of the ubiquitous language and I believe they should live outside of the domain, for example in the Controller.
I would feel more confident with something like:
Order order = orderRepository.find(orderId);
OrderLine orderLine = em.find(OrderLine.class, orderLineId);
order.updateQuantity(orderLine, 2);
Though I don't like the idea of interacting directly with an Entity Manager, either. I feel like I'm bypassing the Repository and Aggregate Root responsibilities (because I could, potentially, interact with the OrderLine directly).
How do you work around that?