聚合根。兔子洞有多远
我正在尝试对当前的项目使用存储库模式,并且目前正在尝试对域进行建模并找到聚合根。
我读过“级联删除”规则,该规则规定,如果在删除根时删除成员没有意义,那么它不应该成为根的一部分。
我将使用警察事件作为示例:-
事件(聚合根)- 这可能包含调查人员、每个人员所做的笔记。它还可能包含嫌疑人以及接受采访的日期列表。 是否获得了该事件的闭路电视录像?每次观看闭路电视的日志以及由谁观看? 是否有闭路电视的副本用于证据/法庭等
似乎事件聚合可能会变得巨大,因为似乎一切都取决于该事件。
我的问题是双重的,聚合根应该管理多少,以及根中的根是一个好主意吗?
这可能不是一个特别好的例子,因为您可能永远不会删除像警察事件这样的事情,但我希望它能更好地描述我的问题。
I'm trying to use the Repository pattern for my current project and i'm currently in the process of trying to model the domain and find the aggregate roots.
I've read of the 'Cascading Delete' rule which states that if it doesn't make sense to delete a member when the root is deleted then it shouldn't be part of the root.
I'll use a Police incident as an eample :-
Incident (Aggregate root) - This could contain investigating officers, notes made by each officer. It could also contain suspects with a list of dates that were interviewed.
Was CCTV footage obtained for the incident? A log of each time the CCTV was viewed and by who?
Were copies made of the CCTV for evidence/court etc
It seems like the IncidentAggregate could become huge since it appears that everything hangs on that incident.
My question is twofold, how much should the aggregate root manage and, are roots within roots a good idea?
This may not be a particularly good example since you'd probably never remove something like a police incident but i hope it describes my question better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
聚合通常包含对其他聚合根的引用。当删除包含的聚合时,应删除这些引用,但它们指向的聚合将保留。
用你的比喻来说。我们假设报告只是一个事件聚合的一部分,并且会与聚合一起删除。没有其他聚合可以直接访问这些报告。
但是,事件聚合将引用代表警官、嫌疑人以及闭路电视查看日志条目的聚合。
An aggregate usually contains references to other aggregate roots. These references should be deleted when the containing aggregate is deleted, but the aggregates they point to would remain.
To use your analogy. A report we shall assume is a part of only one incident aggregate, and would deleted along with the aggregate. No other aggregate would directly access these reports.
However, the incident aggregate would reference aggregates representing officers, and suspects, and CCTV viewing logs entries.
聚合是一组具有相同生命周期的对象。
如果您删除了一个事件,您是否也想删除调查人员?不——如果你这样做了,很快就会没有警察了。调查人员不在事件汇总中。
在你列出的其他事情中,嫌疑人、采访、闭路电视等等。答案是——这取决于情况。
这取决于您的问题域。你的系统在做什么?它的范围是什么?它解决什么问题?
如果唯一的工作是跟踪一系列事件,并假设嫌疑人、采访和闭路电视仅因单一事件而出现在系统中,那么是的,将它们全部集中在一起可能是合适的。如果事件被删除,嫌疑人、采访和闭路电视就可以走了。
例如,如果您还跟踪从市中心摄像机网络收集的闭路电视录像档案。也许您试图监控它们的有效性和可靠性。如果是这样,您需要以不同的方式对待闭路电视录像。它将处于具有自己生命周期的不同聚合中。如果您删除了一个事件,您仍然希望保留其他事件和性能指标的闭路电视录像。
聚合中和外的内容取决于您的问题领域。或者更准确地说,它取决于您对问题域解决方案进行建模的方式。
思考生命周期。
An aggregate is a group of objects with the same lifecycle.
If you deleted an incident would you also want to delete the investigating officer? No – if you did you’d soon have no policemen left. The investigating officer is not in the incident aggregate.
Of the other things you list, suspects, interviews, CCTV etc. The answer is – it depends.
It depends on your problem domain. What is your system doing? what is its scope? what problem is it solving?
If it’s only job is to track a string of incidents and assuming suspects, interviews and CCTV are only in the system as a result of a single incident, then yes, having them all in one aggregate could be appropriate. If the incident is deleted the suspects, interviews and CCTV can go.
If, for example, your also tracking archives of CCTV footage collected from a network of city centre cameras. Maybe your trying to monitor their effectiveness and reliability. If so you need to treat CCTV footage differently. It would be in a different aggregate with its own lifecycle. If you delete an incident you still want to keep your CCTV footage for other incidents and performance metrics.
What is in and out of an aggregate depends on your problem domain. Or more precisely it depends on the way you’ve modelled the problem domain solution.
Think lifecycle.
“根中根是个好主意吗?”
我认为简短的回答是否定的。正如 Kurt 所说,您应该保留对其他聚合的引用。需要明确的是,我所说的引用是指识别对象。因此,也许您的事件聚合会有一个属性,例如
Or
其中 OfficeReference 是一个代表官员身份值的类(在幕后可能是一个 Guid)。
如果您的域逻辑需要使用事件官员和调查官员执行操作,您可以将此逻辑抽象为域服务,并使用 IOfficerRepository 使用事件聚合上提供的 ID 来获取官员聚合。
"Are roots within roots a good idea?"
Short answer in my opinion is no. As Kurt says, you should keep references to other aggregates. Just to be clear, by references I mean identifying objects. So perhaps your Incident aggregate would have a property such as
Or
Whereby OfficeReference is a class that represents the value of an officers identity (under the hood would probably be a Guid).
If your domain logic needed to perform actions using both the Incident and investigating officers you would abstract this logic to a domain service and use the IOfficerRepository to fetch the officer aggregates using the IDs provided on the incident aggregate.