DDD:持久聚合 [英] DDD: Persisting aggregates

查看:82
本文介绍了DDD:持久聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑典型的 Order OrderItem 示例.假设 OrderItem Order 集合的一部分,则只能通过Order添加.因此,要将新的 OrderItem 添加到 Order ,我们必须通过存储库加载整个Aggregate,将新的项目添加到 Order 对象并再次保留整个聚合.

Let's consider the typical Order and OrderItem example. Assuming that OrderItem is part of the Order Aggregate, it an only be added via Order. So, to add a new OrderItem to an Order, we have to load the entire Aggregate via Repository, add a new item to the Order object and persist the entire Aggregate again.

这似乎有很多开销.如果我们的订单有10个 OrderItems ,该怎么办?这样,仅添加一个新的 OrderItem ,我们不仅需要读取10个 OrderItems ,而且还应该重新插入所有这10个 OrderItems . (这是吉米·尼尔森(Jimmy Nillson)在其DDD书中采用的方法.每当他要保留聚合时,他都会清除所有子对象,然后再次重新插入它们.这可能会导致其他问题,因为子ID是由于数据库中的IDENTITY列而每次更改.)

This seems to have a lot of overhead. What if our Order has 10 OrderItems? This way, just to add a new OrderItem, not only do we have to read 10 OrderItems, but we should also re-insert all these 10 OrderItems again. (This is the approach that Jimmy Nillson has taken in his DDD book. Everytime he wants to persists an Aggregate, he clears all the child objects, and then re-inserts them again. This can cause other issues as the ID of the children are changed everytime because of the IDENTITY column in database.)

我知道有些人可能会建议在聚合根"应用工作单位"模式,以便它跟踪已更改的内容,仅提交那些更改.但这违反了持久性忽略(PI)原理,因为持久性逻辑正在泄漏到域模型中.

I know some people may suggest to apply Unit of Work pattern at the Aggregate Root so it keeps track of what has been changed and only commit those changes. But this violates Persistence Ignorance (PI) principle because persistence logic is leaking into the Domain Model.

以前有人想过吗?

Mosh

推荐答案

我不确定这种方法100%正确,但是我认为应用工作单元模式可能是答案.请记住,应在应用程序或域服务中完成任何事务,您可以使用更改后的聚合中的对象填充工作单元类/对象.之后,让UoW类/对象发挥作用(当然,在某些情况下构建适当的UoW可能很难)

I'm not 100% sure about this approach , but I think applying unit of work pattern could be the answer . Keeping in mind that any transaction should be done , in application or domain services , you could populate the unit of work class/object with the objects from the aggregate that you have changed . After that let the UoW class/object do the magic (ofcourse building a proper UoW might be hard for some cases)

这是此处

工作单元会跟踪您在业务交易过程中可能影响数据库的所有操作.完成后,它会计算出由于工作而需要更改数据库的所有工作.

A Unit of Work keeps track of everything you do during a business transaction that can affect the database. When you're done, it figures out everything that needs to be done to alter the database as a result of your work.

这篇关于DDD:持久聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆