domain-driven-designclean-architectureaggregateroot

How to update related entity in clean architecture


my problem is about updating an entity related with the aggregate root. I have a Shipping class and my command is SetShipmentAsDelivered. Shipment has a relation with the Order class. So when the shippent is delivered I should mark the order as delivered.

My flow is like this;

  1. Application layer makes a call to the Infra layer to get a ShipmentModel, ShipmentModel is mapped from database entity (EF Core)
  2. In the application layer make method call to the ShipmentModel like SetAsDelivered to update delivery related fields (time, price etc)
  3. In application layer make a call to Infra layer to save the ShipmentModel, it is mapped to the EF Core entity to get saved to the database.

So far I've no problem. But for the requirement mark the order as delivered I have to map all Order entity inside the ShipmentModel (like shipmentModel.Order.SetStateDispatched) which seems so redundant because all I need is making an update at one field for the related entity "Order". Should I just seperate to save operations and create the Order class out of the Shipment? What is the best way to handle such scenario?

Thank you.


Solution

  • You can use domain events.

    So the use case that updates the shipment can emit an order shipped event.

    In the order component you can implement an domain event listener that receives the event and executes the order update use case.

    The MarkOrderShipped use case is independent from the way it is triggered. It might be the domain event listener or a controller that is invoked as a result of a user action.

    I published an example application in Java that uses an domain event bus to synchronize different components. It doesn't matter if that domain event bus is a local kind of synchronous event delivery mechanism, an asynchronous local or a message queue or something else.