聚合根如何删除其子节点之一?

发布于 2024-08-11 04:37:56 字数 236 浏览 6 评论 0原文

如果我对聚合根的理解是正确的,那么根也应该负责删除它的“子项”之一。这似乎会转化为这样的内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

触ぅ动初心 2024-08-18 04:37:56

您的工作单元通常应该处理这个问题,但这取决于它的实现,特别是它检测更改的方式。某些工作单元实现(即 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文