在 CQRS 中,如何将聚合上允许的方法列表传达给 UI?
我有一个聚合根“订单”,它有许多方法在内部设置其“状态”字段:
- 提交
- 、暂停、
- 起飞、
- 确认、
- 取消
- 等。
可用的操作取决于订单的当前状态(例如,如果已经处于搁置状态,则不能将其搁置)。问题是我需要提供一个查询来告诉 UI 哪些命令可用,以便我可以隐藏否则会引发 InvalidOperationException
的操作。
如何在最小化 DRY 违规的情况下做到这一点?
I have an aggregate root 'Order', and it has a number of methods on it which internally set its 'Status' field:
- Submit
- Place On Hold
- Take Off Hold
- Confirm
- Cancel
- etc.
The available actions are dependent upon the current Status of the order (e.g. it can't be put on hold if it's already on hold). The problem is that I need to provide a query to tell the UI which commands are are available so I can hide the operations that would otherwise throw an InvalidOperationException
.
How do I do this with minimal DRY violation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的解决方案是投影当前状态以及可用于读取/查询模型的转换,并选择它以及要显示的数据。
例子:
PlaceOnHoldCommand
会产生OrderPlacedOnHoldEvent
,该事件(除了放入 EventStore 中)由OrderTransitionsEventHandler
发布和处理,从而对可用转换进行非规范化到与订单关联的数据库表。客户端选择可用的转换并进行相应的操作(隐藏不可用的按钮或类似的东西)。这当然是选项之一。但不要指望不会有任何重复。 CQRS 有助于管理复杂性,有时这意味着会发生轻微的 DRY 违规。
The simplest solution is to project current status along with available transitions to read/query model and select it along with the data to display.
Example:
PlaceOnHoldCommand
results inOrderPlacedOnHoldEvent
which (besides being put in EventStore) is published and handled byOrderTransitionsEventHandler
which denormalizes available transitions to a DB table associated with Order. The client selects available transitions and acts accordingly (hides unavailable buttons or sth. like that).This is of course one of options. Don't expect however that there will be no duplication whatsoever. CQRS helps to manage complexity and sometimes this means slight violations of DRY occur.