聚合根如何删除其子节点之一?
如果我对聚合根的理解是正确的,那么根也应该负责删除它的“子项”之一。这似乎会转化为这样的内容:
order.removeOrderLine(23);
这将有效地将其从集合中删除。然而,这是如何坚持下来的呢?我的 ORM 的 UnitOfWork 是否应该检测到该集合中缺少某些内容,然后将其从数据库中删除?
我应该使用 OrderRepository 的方法来删除订单行吗?
If my understanding of Aggregate Roots is correct, the root should be responsible also for deleting one of its "children". That would seemingly translate into something like this:
order.removeOrderLine(23);
Which would effectively remove it from the collection. However, how is this persisted? Is my ORM's UnitOfWork supposed to detect that something went missing in that collection, and then delete it from the database?
Should I have removeOrderLine a method of the OrderRepository instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的工作单元通常应该处理这个问题,但这取决于它的实现,特别是它检测更改的方式。某些工作单元实现(即 Hibernate)会在您更改聚合之前保留聚合的副本,因此在业务事务结束时(当您调用诸如 unitOfWork.PersistAll() 之类的东西时),它会尝试匹配所有对象的当前版本(和收藏)与原始版本相对应。
另一种方法是让您的域实体与您的工作单元更加耦合,以便实体在发生更改时通知工作单元(即 order.removeOrderLine 方法将通知工作单元有关更改)。
有多种方法可以实现 UoW 变更检测。查看 hibernat 的几种实现以获取灵感。
Your Unit of Work should usually take care of this, but it depends on its implementation, specifically on the way it detects changes. Some unit of work implementations (i.e. Hibernate) keeps a copy of the aggregate before you changed it, so at the end of business transaction (when you call something like unitOfWork.PersistAll()), it tries to match current version of all objects (and collections) against the original version.
Other way is to have your domain entities more coupled with your unit of work so that the entity notified the unit of work when something changes (i.e. the order.removeOrderLine method would notify the unit of work about the change).
There are multiple ways how to implement UoW change detection. Have a look at several implementations for hibernat for inspiration.