域驱动设计集合引用问题 [英] Domain Driven Design Aggregate Referencing Question

查看:153
本文介绍了域驱动设计集合引用问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有 Eric Evans的域驱动的设计书


外部对象可能不持有对内部
的实体的引用到总计。外部对象必须仅引用
聚合根,没有内部对象。

External objects may not hold a reference to an entity that is internal to the aggregate. External objects must reference the aggregate root only, no internal objects.

例如,我的团队聚合根具有一个名为 AddPlayer()的方法,并返回添加的 Player 实体。这是否意味着我违反了这个规则,还是说我不能从简单的空气中拉出一个 Player 实体,例如从集合边界之外的存储库中拉出? / p>

For example, my Team aggregate root has a method called AddPlayer() and returns the Player entity added. Does this mean I am violating the rule or is it saying I cannot pull a Player entity out of thin air, for example pulling it from a repository outside of its aggregate boundary?

推荐答案

这总是一个棘手的问题,很有可能表明您的设计不太适合您的域名。我有一点要说我的博客(你应该感兴趣):

This is always a tricky issue and is most likely an indication that your design does not quite fit your domain. I have a bit to say about this on my blog (should you be interested):

http://www.ebenroux.co.za/post/2010/08/20/Natrual-Aggregates- vs-Synthetic-Aggregates.aspx

你有一个团队,你有一个播放器。那将是2个聚合根。使团队的聚合根和玩家只是一个包含的实体是什么可能导致你的痛苦。在现实生活中,玩家不需要属于一个团队,也取决于你的团队。这只是集体的名字,还是目前的会员,还是在某一天可以带到现场的实际人物?

You have a Team and you have a Player. That would be 2 Aggregate Roots. Making Team the Aggregate Root and Player just a contained entity is what is probably causing you pain. In real life a player need not belong to a team and it also depends on what your 'team' is. Is it just the collective name, or the current members, or the actual folks that can take to the field on a given day?

所以你最终可能会有不同的团队:

So you will probably end up with different 'teams':


  • 团队

  • 团队

  • GameSquad

所以玩家不一定是集合的部分,而是聚合可以有某种所有权对玩家来说可能是一个很弱的参考(例如只有一个ID或一些价值对象)。这样的事情

So the players are not necessarily part of the aggregate but rather the aggregate can have some kind of ownership with probably a rather weak reference to the player (such as only an ID or some value object). Something to that effect.

但是要回到Eric在他的书中提到的内容:我认为它与这样的东西(使用你的模具)有关:

But to get back to what Eric refers to in his book: I think it relates to something like this (using your mould):

var line = Order.AddLine(SomeProduct);

var line = Order.AddLine(SomeProduct);

这里不应该太多的意义,引用了实体中的实体因为它没有它自己的生命周期。那么在这种情况下,订单行甚至不是一个实体。

Here it shouldn't make too much sense having a reference to the actual entity within the aggregate as it has no lifecycle of it's own. Well, in this case the order line is not even an entity.

还有一些讨论是否存储库是否返回AR或实体(在某些存储库中,是AR)。根据什么找到蓝皮书,你可以从存储库中检索一个实体。

There has also been some discussion around whether repository only return ARs or Entities (that, in some repositories, are ARs). According to what I have found the blue book your can retrieve an entity from a repository.

无论如何。只是一些想法HTH:)

Anyway. Just some thoughts. HTH :)

这篇关于域驱动设计集合引用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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