如何使用实体框架对聚合进行建模?

发布于 2024-09-18 10:40:58 字数 325 浏览 0 评论 0原文

虽然我处理域驱动设计 (DDD) 已经有一段时间了,但我对实体框架 (EF) 还比较陌生,在 Visual Studio 中使用实体框架设计器时我想到的一个问题是如何聚合应在 EF 中表示/建模。

遵循 DDD 最佳实践,实体应仅引用同一聚合内的其他实体(或值对象),并且对其他实体的引用仅限于聚合的根实体(聚合根)。但是,我在 EF 中没有看到任何这些概念(即所有实体都被同等对待,因此实体之间的引用没有受到任何限制)。

因此,我问:我是否错过了 EF 中的某些内容,或者它对聚合、聚合根和实体之间的引用完全不可知?如果是后者,那么在使用时如何对聚合进行建模实体框架?

While I have been dealing with domain-driven design (DDD) for quite some time now, I'm relatively new to Entity Framework (EF), and one question that came to my mind when using the Entity Framework Designer in Visual Studio was how Aggregates should be represented/modeled in EF.

Following DDD best practices, Entities should only reference other Entities (or Value Objects) within the same Aggregate, and references to other Entities are restricted to Root Entities of Aggregates (Aggregate Roots). However, I don't see any of these concepts present in EF (i.e., all Entities are treated alike, and consequently no restrictions are applied on references between Entities).

Thus, I'm asking: did I miss something in EF, or is it completely agnostic about Aggregates, Aggregate Roots and references between Entities? If the latter is the case, how do you model Aggregates when using Entity Framework?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

卷耳 2024-09-25 10:40:58

我认为 DDD 是其他级别的抽象,所以我的答案是 EF 默认情况下不遵循这些实践。您可以根据 DDD 对实体和存储库进行建模。您将使用存储库来构建聚合根,其中加载的相关实体仅与当前聚合根相关,并且您将使用域服务来处理不同的存储库。

I think DDD is other level of abstraction so my answer is no EF by default does not follow these practicies. It is up to you to model your entities and repositories to follow DDD. You will use repositories to build your aggregate root with loaded related entities related only to current aggregate root and you will use domain services to work with different repositories.

放血 2024-09-25 10:40:58

我只是想纠正一个小(但非常重要的细节):

您声明“实体应该只引用同一聚合中的其他实体(或值对象)”。

当然,对此可能存在一些争论,但它比 DDD 上至少一项资源所建议的更严格:“聚合内的对象应该被允许保存
对其他聚合的根的引用。”(Avram 和 Marinescu 的“快速领域驱动设计”)。

最诚挚的问候,
西蒙

I just wanted to correct a small (but pretty important detail):

You state that "Entities should only reference other Entities (or Value Objects) within the same Aggregate".

Of course, there could be some arguments for this, but it's stricter than what at least one resource on DDD recomends: "Objects inside an Aggregate should be allowed to hold
references to roots of other Aggregates." ("Domain-Driven Design Quickly" by Avram & Marinescu).

Best regards,
Simon

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