简体   繁体   English

如何定义聚合的一对多关系

[英]How to define aggregate one-to-many relationship

I have two aggregates Blog and Post with a one-to-many relationship ( Blog is related to several Post ).我有两个聚合BlogPost具有一对多的关系( Blog与几个Post相关)。 According to domain driven design best practices I am unsure if Blog should hold a collection of Post references:根据领域驱动设计最佳实践,我不确定Blog是否应该包含一系列Post参考:

public class Blog : Entity<BlogId>, IAggregateRoot
{
    IEnumerable<Post> posts;
}

when Post also has an indirect id reference through BlogId :Post还通过BlogId具有间接 id 引用时:

public class Post : Entity<PostId>, IAggregateRoot
{
    BlogId BlogId;
}

If more context is required in order to answer the question please let me know.如果需要更多上下文来回答问题,请告诉我。

In the book by Vaughn Vernon, Implementing Domain-Driven Design, you can find a entire chapter on how to define your aggregates and some rules of thumb you can apply.在 Vaughn Vernon 所著的《实现领域驱动设计》一书中,您可以找到一整章介绍如何定义聚合以及可以应用的一些经验法则。

One of them is to try to keep your aggregates as small as possible.其中之一是尽量保持你的聚合尽可能小。 In your case, I would opt to avoid having a list of Post objects inside your Blog object.在您的情况下,我会选择避免您的博客 object 中包含 Post 对象列表。 Imagine that your Blog object has an attribute name and you just want to change it: when you will retrieve that object from the database, if it contains all of the Posts, you will retrieve them too, making an unnecessary join for the use case you are working on, and that will decrease the performance.想象一下,您的博客 object 有一个属性name ,而您只想更改它:当您将从数据库中检索该 object 时,如果它包含所有帖子,您也会检索它们,从而为您的用例创建不必要的连接正在努力,这将降低性能。

From a design point of view, probably that Blog is not defined by its Post, so actually you don't need them inside it.从设计的角度来看,Blog 可能不是由它的 Post 定义的,所以实际上你不需要它们在里面。 An aggregate should be responsible of all of its invariant rules (business logic rules) and no more.聚合应该负责其所有不变规则(业务逻辑规则),仅此而已。

My advice is to just keep it as simple as possible and always focus on solid principles.我的建议是让它尽可能简单,并始终专注于坚实的原则。

Good luck!祝你好运!

DDD does not tell you how to implement your application, but gives you just guidelines. DDD 不会告诉您如何实现您的应用程序,而只是为您提供指导。 Holding a list of "posts" into a "blog" class and a logic reverse refernce is definetly ok if you cannot do something else, but the most important thing to understand is how a "blog user" will be able to act upon posts.将“帖子”列表保存到“博客”class 和逻辑反向引用绝对可以,如果您不能做其他事情,但最重要的是要了解“博客用户”将如何对帖子采取行动。

Thus, feel free to implment the domain model in your most preferred way, but the only way to add posts, delete post, etc will be through commands sent to the "blog" entity, so that will be the blog itself to grant you that the posts are wired correctly according with your implementation因此,请随意以您最喜欢的方式实现域 model,但添加帖子、删除帖子等的唯一方法是通过发送到“博客”实体的命令,这样博客本身就会授予您这些帖子根据您的实施正确连接

A rule for aggregates is that you should reference an aggregate by its aggregate root id, so in Blog aggregate you should keep a list of Post ids.聚合的规则是您应该通过聚合根 ID 引用聚合,因此在博客聚合中您应该保留一个帖子 ID 列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM