DDD:在聚合根内模拟本地身份

发布于 2024-12-06 11:32:27 字数 564 浏览 1 评论 0原文

阅读蓝皮书(Eric Evan 的领域驱动设计)并开始在类似博客的应用程序中应用 DDD 概念后,我有以下问题,如何对聚合根内的实体的本地身份进行建模?

为了简单起见,我有一个简单的博客模型,其中包含以下实体和场景: 注册用户可以发布帖子,帖子可以具有一个或多个关联标签,并且注册或未注册用户可以发布对帖子的评论。

在这种情况下,实体是用户、帖子和评论,聚合根是用户和帖子,评论是帖子聚合根内的聚合。

那么,由于评论实体在 Post 中具有本地身份,我如何建模其本地身份? IE 我不能仅通过评论的属性来区分评论,因为我可以对同一用户发布的具有相同内容的同一篇文章有​​不同的评论...

我首先想到了帖子评论列表中评论的顺序来识别在帖子内发表评论,但这在并发环境中变得非常复杂,例如一个 Web 应用程序,两个客户端可以在博客上发表评论,并且在将它们存储在数据库中之前我会发生冲突。除此之外,我需要保留从存储库恢复评论列表的逻辑,其顺序与帖子保存到存储库的顺序相同...

然后在帖子内有一个唯一的标识符计数器,并在发布的每个评论中自动递增它,但在并发环境中变得复杂,那么如何在聚合根内对本地身份进行建模?

谢谢 巴勃罗

after reading the blue book (Eric Evan's Domain Driven Design) and starting applying the DDD concepts in a simple blog like application I have the following question, how do you model a local identity of an entity inside an aggregate root??

Let's say for the sake of simplicity, I have a simple blog model that has the following entities and scenarios:
A registered user can publish a post, a post can have one or more tags associated and a registered or unregistered user can publish a comment on a post.

In this scenario, the entities are User, Post, and Comment, the aggregate roots are User na Post being comment an aggregate inside Post aggregate root.

So since the comment entity has a local identity inside Post how do I model its local identity?? I.E. I cannot differenciate a comment just by it's attributes since I can have twho different comments for the same post published by the same user with the same content...

I first thought of the order of the comment inside the post's comment list to identify the comment inside the post, but this becomes very complex in concurrent environments, like a web application where two clients can post comments on blogs and I would have collisions until I store them in the database. Besides that I need to keep the logic for restoring the comment list from repository in the same order the post was saved to the repository...

Then having a unique identifier counter inside the Post and autoincrement it with every comment published, but in concurrent environment becomes complex, so how do I model the local identity inside an aggregate root??

Thanks
Pablo

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

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

发布评论

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

评论(2

つ可否回来 2024-12-13 11:32:27

好问题。来自 Eric Evan 的书:

边界内的实体具有本地身份,仅在边界内是唯一的
聚合。

...只能通过数据库查询直接获得AGGREGATE根。所有其他对象必须通过遍历关联来找到。

我认为第二部分才是重要的。您应该评论 ID 视为本地身份。换句话说,您不应该绕过其聚合根(帖子)检索评论,引用来自外部的评论等。从技术上讲,Commend Id 可以是数据库生成的相同自动增量字段,就像您对用户和帖子(或任何其他 id)所拥有的那样来自休眠的生成器)。但从概念上来说,Comment 将具有本地特性。解决这个问题是聚合根的工作。

Good question. From Eric Evan's book:

ENTITIES inside the boundary have local identity, unique only within
the AGGREGATE.

...only AGGREGATE roots can be obtained directly with database queries. All other objects must be found by traversal of associations.

I think that the second part is what is important. You should be treating Comment Id as local identity. In other words you should not retrieve Comments bypassing its Aggregate Root (Post), reference Comments from the outside etc. Technically, Commend Id can be the same AUTOINCREMENT field generated by database, like you would have for User and Post (or any other id generator from hibernate). But conceptually Comment will have local identity. It is the job of the Aggregate Root to resolve it.

舂唻埖巳落 2024-12-13 11:32:27

我不是 DDD 专家,但我认为 Comment 或许应该是一个值对象。值对象没有“概念标识”,但它当然可以具有持久标识。通常,您应该更喜欢使用值对象而不是实体,因为后者通常具有更多开销。

不要将持久性同一性与概念性同一性混淆。一开始我很难理解这一点。

I'm no DDD expert, but I would argue that perhaps Comment should be a value object. A value object has no "conceptual identity," but it can certainly have a persistence identity. Usually, you should prefer using value objects over entities because the latter typically has more overhead.

Don't confuse persistence identity with conceptual identity. I had trouble understanding that in the beginning.

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