DDD中的多对多关系 [英] Many-to-many relationships in DDD

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

问题描述

我是DDD的新手,我与多对多关系保持联系。例如。我们有两个聚合根-任务和工作者。

I'm new to DDD and I'm stuck with many-to-many relationships. E.g. we have two aggregate roots - Tasks and Workers.

合同绝对不是聚合根,因为没有任务和工作者就没有意义。因此,它应该是某些集合的一部分。但是它应该属于哪个集合?我们需要知道所有任务合同的汇总成本和所有工人合同的汇总成本。对我来说,在Task和Worker中都可以收取合同是很自然的。

Contract is definitely not aggregate root, because it has no sense without Task and Worker. So, it should be part of some aggregate. But which aggregate should it belong to? We need to know both summary costs of all task contracts and summary costs of all worker contracts. And it's natural for me to have contracts collection both in Task and in Worker.

好吧,我可以将成本计算移到域服务中,但是我担心这是向贫乏模型迈出的一步。有没有通用的方法来处理多对多关系并保留到达域模型?

Well, I can move Costs calculation to domain service, but I afraid it's a step forward to anemic model. Is there common way to deal with many-to-many relationships and preserve reach domain model?

谢谢!

推荐答案

通过在边栏中链接以下相关问题,我发现了这篇有趣的文章:

By following related questions linked in the sidebar, I found this interesting article:

DDD&多对多对象关系映射

似乎建议我无论如何都凭直觉进行思考:实际上,让工人和任务承担责任并不自然对合同的依赖。也就是说,没有合同的概念(以及类似的任务),工人的概念仍然有意义,因此体现该概念的实体不应依赖于合同实体。

It seems to recommend what I was intuitively thinking anyway: that it is not in fact natural for worker and task to take on a dependence on contract. That is, the concept of a "worker" is still meaningful without the concept of a "contract" (and similarly for task), so the entity embodying that concept should not have a dependency on the contract entity.

要显示分配给给定任务的合同或分配给给定工作人员的合同,您将需要运行域查询。实际上,这实际上是域名服务的一种适当用法,如果您考虑一下,它可以更好地反映您的域名的真实情况。

To display the contracts assigned to a given task, or the contracts assigned to a given worker, you will need to run a domain query. This is in fact an appropriate use for a domain service, and better reflects the reality of your domain if you think about it.

我还注意到您说的合同是绝对不能聚合根,因为没有Task和Worker毫无意义。实际上,这就是Contract 是总根的确切原因。

I also note that you say "Contract is definitely not aggregate root, because it has no sense without Task and Worker." That is actually the precise reason that Contract is the aggregate root.

因此,我的建议是,从arootbeer的见解中得出结论:

So, my suggestion, with arootbeer's insight from the comments incorporated:

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

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