从值对象访问AR(Accessing AR from value object)

我有一个很难解决的问题。 在我的模型中,我有AR Unit , AR Stage和VO GoToPositionOrder ,它实现了Order接口。

它的工作方式如下:

我创建了order: order = GoToPositionOrder(Position(Point(3, 4))) 我把它给单位: unit.followOrder(order) (我可以给单位发出各种订单) 订单存储在Unit中,然后我可以存储unit: unitRepository.store(unit) 存储在单元中的订单每个步骤都跟着单元,直到订单完成,所以每次发送事件TimeStep ,我都会调用域服务单元unitsFollowOrders(unitRepository.all())

现在,问题在哪里? 遵循以下命令对给定单位(命令模式)执行某些操作: order.execute(unit) 。 问题是不同的订单需要不同的附加数据来执行其操作。 例如, GoToPositionOrder需要访问AR Stage以便找到最短的定位路径。 但是我怎样才能让Order访问Stage ?

我不能简单地在那里传递引用,因为AR应该由id引用,原因有多种。 如果它被id引用,那么为了检索它,VO将有权访问存储库,这违反了SRP(单一责任原则)。

我还有其他选择吗?

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?

最满意答案

我认为域服务是放置寻路逻辑的正确位置,因为该功能似乎不属于任何实体,是无状态的,并且需要来自多个AR的数据。

它将有一个FindShortestPath()方法生成一个GoToPositionOrder VO,然后将其注入Unit 。 VO不包含逻辑,只包含为了实现目标而采取的步骤清单。

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.

从值对象访问AR(Accessing AR from value object)

我有一个很难解决的问题。 在我的模型中,我有AR Unit , AR Stage和VO GoToPositionOrder ,它实现了Order接口。

它的工作方式如下:

我创建了order: order = GoToPositionOrder(Position(Point(3, 4))) 我把它给单位: unit.followOrder(order) (我可以给单位发出各种订单) 订单存储在Unit中,然后我可以存储unit: unitRepository.store(unit) 存储在单元中的订单每个步骤都跟着单元,直到订单完成,所以每次发送事件TimeStep ,我都会调用域服务单元unitsFollowOrders(unitRepository.all())

现在,问题在哪里? 遵循以下命令对给定单位(命令模式)执行某些操作: order.execute(unit) 。 问题是不同的订单需要不同的附加数据来执行其操作。 例如, GoToPositionOrder需要访问AR Stage以便找到最短的定位路径。 但是我怎样才能让Order访问Stage ?

我不能简单地在那里传递引用,因为AR应该由id引用,原因有多种。 如果它被id引用,那么为了检索它,VO将有权访问存储库,这违反了SRP(单一责任原则)。

我还有其他选择吗?

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?

最满意答案

我认为域服务是放置寻路逻辑的正确位置,因为该功能似乎不属于任何实体,是无状态的,并且需要来自多个AR的数据。

它将有一个FindShortestPath()方法生成一个GoToPositionOrder VO,然后将其注入Unit 。 VO不包含逻辑,只包含为了实现目标而采取的步骤清单。

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.