创建与其他聚合关系的聚合时的DDD一致性检查(DDD consistency check when creating aggregates with relationship to another a

编程入门 行业动态 更新时间:2024-10-14 16:19:38
创建与其他聚合关系的聚合时的DDD一致性检查(DDD consistency check when creating aggregates with relationship to another aggregates)

在创建与另一个聚合关系有关的新聚合时,我应该在哪里检查关系聚合是否存在? 它应该在应用程序服务中,还是在某些域服务的工厂内部?

class ApplicationService { public void CreateNewAr(relationArId, relationArId2) { var relationAR = _relationArRepository.getById(relationArId); if(relationAR == null) throw NotFoundException(); var relationAR2 = _relationAr2Repository.getById(relationArId2); if(relationAR2 == null) throw NotFoundException(); var newAr = _newArFactory.CreateFromAr1And2(relationAR.id, relationAR2.id); _newArRepository.Insert(newAr); _uow.Commit(); } }

要么

class NewArFactory { public NewAr CreateFromAr1And2(relationArId, relationArId2) { var relationAR = _relationArRepository.getById(relationArId); if(relationAR == null) throw NotFoundException(); var relationAR2 = _relationAr2Repository.getById(relationArId2); if(relationAR2 == null) throw NotFoundException(); var newAr = new NewAr(relationAR.id, relationAR2.id); return newAr; } }

When creating new aggregate which has a relation to another aggregates, where should I check do relation aggregates exist? Should it be in the application service, or inside factory by some domain service?

class ApplicationService { public void CreateNewAr(relationArId, relationArId2) { var relationAR = _relationArRepository.getById(relationArId); if(relationAR == null) throw NotFoundException(); var relationAR2 = _relationAr2Repository.getById(relationArId2); if(relationAR2 == null) throw NotFoundException(); var newAr = _newArFactory.CreateFromAr1And2(relationAR.id, relationAR2.id); _newArRepository.Insert(newAr); _uow.Commit(); } }

or

class NewArFactory { public NewAr CreateFromAr1And2(relationArId, relationArId2) { var relationAR = _relationArRepository.getById(relationArId); if(relationAR == null) throw NotFoundException(); var relationAR2 = _relationAr2Repository.getById(relationArId2); if(relationAR2 == null) throw NotFoundException(); var newAr = new NewAr(relationAR.id, relationAR2.id); return newAr; } }

最满意答案

TL; DR:去寻找域名。 工厂与否是另一个讨论。

现在,您是否意识到,如果NewArFactory控制域中的更改, NewArFactory是一个聚合?

请记住,聚合的目的是控制变化。 它可以是一个可能的实现细节,但并不总是需要持久化聚合本身。 从持久性中检索聚合时,不总是意味着您应该有一个聚合表(或在多个表中拆分)。 存储库可以使用持久性中的信息获取构建聚合所需的数据(即实体 - VO ID),并将其返回。 聚合(通过聚合根)控制实体中的更改,然后保持新状态。

域中的事物,比如现实生活中的事物,并不是从空气中出现的。 试着了解发生了什么。 汽车组装,产品从中国到达我们的商品,用户在我们的网络应用程序中注册等等。然后为您的域名表示的创建提供更好的背景(使用域无所不在的语言涉及的用例和过程)。 从那里; 将出现一个聚合来控制变化。

可能是自我创建,使用另一个聚合等等。即使是一个没有持久化的聚合也可能会创建一个持久的聚合... Uff,如果没有表中的完整真实域上下文,这个事情变得非常复杂。

注意:在回答这个问题时,没有任何聚合损坏也不会持续存在。

TL;DR: Go for the domain. Factory or not it is another discussion.

Now, did you realize that, if NewArFactory is controling a change in the domain, NewArFactory is an aggregate?

Remember that the purpose of an aggregate is control the change. It can be a possible implementation detail but not always an aggregate itself needs to be persisted. When you retrieve an aggregate from persistence does not, always, mean that you should have a aggregate table (or splited in several tables). A repository could get the data (i.e. entities - VOs ids) needed to build an aggregate, using the info in persistence, and return it. The aggregate (through aggregate root) controls the change in the entities and then the new state is persisted.

Things in a domain, like in real life, does not appear from thin air. Try to understand what is happening. A car is assembled, a product arrives from china into our warehause, a user registered himself in our web app, etc. Then provide a better context to the creation of your domain representation (use case and process involved using domain ubiquitous language). From there; an aggregate will emerge to control the change.

Could be self creation, use another aggregate, etc. Even one aggregate that is not persisted could create an aggregate that is persisted... Uff, this thing get very complex to explain without full real domain context in the table.

Note: No aggregate was harmed nor persisted in the making of this answer.

更多推荐

本文发布于:2023-08-04 22:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423426.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关系   与其他   一致性   DDD   consistency

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!