Search code examples
domain-driven-designaggregaterootvalue-objects

Accessing AR from value object


I have a quite difficult problem to solve. In my model I have AR Unit, AR Stage and VO GoToPositionOrder, that implements Order interface.

It works like that:

  • I create order: order = GoToPositionOrder(Position(Point(3, 4)))
  • I give it to unit: unit.followOrder(order) (I can give various of orders to unit)
  • orders are stored in Unit and then I can store unit: unitRepository.store(unit)
  • orders that are stored in unit are followed by unit every step, until order is finished, so every time event TimeStep is sent, I call domain service unitsFollowOrders(unitRepository.all())

Now, where is the problem? Each order does some action on given unit (command pattern) when is followed : order.execute(unit). Problem is that different orders needs different additional data to do its action. In example GoToPositionOrder needs access to AR Stage so it can find the shortest path to position. But how can I give Order access to Stage?

I can't simply pass a reference there, because AR should be referenced by id, for various reasons. If it's referenced by id, then to retrieve it, VO would have access to repository, and that violates SRP (single responsibility principle).

What other options do I have?


Solution

  • I think a Domain Service is the right place to put the pathfinding logic since that capability doesn't seem to belong in any Entity, is stateless, and needs data from multiple AR's.

    It would have a FindShortestPath() method producing a GoToPositionOrder VO which is then injected into the Unit. The VO would contain no logic, only the list of steps to take for the Unit to reach its goal.