绕过聚合根

发布于 2024-07-27 01:28:25 字数 599 浏览 4 评论 0原文

是否可以从聚合中获取只读集合而不通过根来获取它? 我的模型现在做了一些这样的事情,我想知道这是否是一个可以接受的设计。 谢谢

编辑:

这是一个示例,

我有一个名为 UserAccount 的聚合根实体和另一个名为 VideoStore 的聚合根实体。 用户可以拥有多个他们所属的商店,并且视频商店可以拥有许多用户。 一个非常基本的多对多,但这并不是因为多对多桥接表包含状态信息,所以它也必须是一个实体。 因此,我有一个名为 UserVideoStores 的桥接实体,它是聚合根 VideStore 的子实体(一对多)。

现在,当用户登录时,我想查找他们所属的 VideoStore 并向他们显示该信息。 通过使 UserAccount 实体具有对聚合根 VideoStores 的子 UserVideoStores 的直接(一对多)引用,我可以轻松地做到这一点。 这样做似乎更容易,然后必须使用 HQL 查询并从图表底部向上搜索以查找用户所在的商店。

那有意义吗?

编辑:

嗯,我想出了一个让我的模型更干净的解决方案。 我没有直接思考我的一些设计,我学会了如何更好地使用 nHibernate 来帮助我提出解决方案。 谢谢

Is it okay to get a read-only collection from an aggregate without going through the root to get it? My model does some of this right now and I was wondering if that's an acceptable design. Thanks

Edit:

Here's an example

I have an aggregate root entity called UserAccount and another aggregate root called VideoStore. Users can have multiple stores they are apart of and video stores can have many users. A very basic many-to-many, but it's not because the many-to-many bridge table contains state information so it has to be an entity as well. So, I have an bridge entity called UserVideoStores and its a child of the aggregate root VideStore (one-to-many).

Now when a user logs in I want to lookup which VideoStores they are apart of and display that info to them. I can easily do this by making the UserAccount entity have a direct (one-to-many) reference to the child, UserVideoStores, of the aggregate root VideoStores. It seems easier to do this then have to use an HQL query and search from the bottom of the graph up to find which stores the user is apart of.

Does that make sense?

Edit:

Well I came up with a solution to make my model cleaner. I wasn't thinking straight about some of my designs and I learned how to use nHibernate a little bit better to help me come up with a solution. Thanks

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

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

发布评论

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

评论(2

记忆之渊 2024-08-03 01:28:25

Evans 说“根是允许外部对象保存对...的引用的 AGGREGATE 的唯一成员”(第 127 页)

我的理解是聚合应该作为外部对象的一个​​单元出现。 此外,德米特法则似乎也适用。 归根结底,我认为这是不可接受的。

Evans says "The root is the only member of the AGGREGATE that outside objects are allowed to hold references to..." (p. 127)

My understanding is that the aggregate should appear as a unit to outside objects. Also, the Law of Demeter would seem to apply. Bottom line, I don't think it's acceptable.

╰沐子 2024-08-03 01:28:25

事实上,埃里克已经改变了他对聚合根规则严格性的看法。
他最近说了这样一句话:

“冷静点,伙计。试着阻止自己违反规则。但是,嘿,不要为此自责。DDD 很酷,伙计,但如果你真的需要的话,请打破 Agg 规则。”是啊,平安。”

看:
“Eric Evans:自从这本书以来我对 DDD 的了解”
http://dddcommunity.org/library/evans_2009_1

Actually Eric has changed his mind about the strictness of the Aggregate Root Rules.
He recently said something along the lines of

"Chill out dude. Try to stop yourself from breaking the rules man. But, hey, don't beat yourself up over it. DDD rocks dude, but break the Agg rules if ya really need to. Yeah, peace."

See:
"Eric Evans: What I've learned about DDD since the book"
http://dddcommunity.org/library/evans_2009_1

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