正确的聚合根是什么?
我有以下数据结构:
class Post
{
public string Title { get; set;}
public Category Category { get; set;}
}
记住一篇文章总是属于一个且仅一个类别,我的推理是否正确?
Category
是聚合根(因为没有它,帖子就无法存在)- 应该有一个
CategoryRepository
(具有诸如GetCategory< /code> 和
GetPost
),但不是PostRepository
(因为Post
不是聚合根)
I have the following data structure:
class Post
{
public string Title { get; set;}
public Category Category { get; set;}
}
Bearing in mind a post always belongs to one and only one category is my reasoning correct?
Category
is the aggregate root (since a post cannot exist without it)- There should be a
CategoryRepository
(with methods such asGetCategory
andGetPost
) but not aPostRepository
(becausePost
is not an aggregate root)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想如何引用
Post
的实例?Post.Title
是Post
的唯一标识符吗?如果是这样,那么Post
是一个有效的聚合根,您应该创建一个PostRepository
来检索Post
的实例给定它的标题
。以汽车为例。汽车必须有颜色,但仅仅因为汽车没有颜色就无法存在,就说颜色是聚合根,这是错误的做法。我想根据汽车的车牌号(与其颜色无关)独立地引用一辆汽车。它必须有颜色这一事实只是我的汽车域模型的一个特征,它表明我无法在不提供颜色的情况下构建汽车实例。
How do you want to reference instances of
Post
? IsPost.Title
a unique identifier for aPost
? If so, thenPost
is a valid aggregate root and you should create aPostRepository
which retrieves an instance ofPost
given it'sTitle
.Take the example of a car. A car must have a colour, but stating that the colour is the aggregate root just because a car cannot exist without one is the wrong thing to do. I want to reference a car independently given it's license plate number (which is independent of it's colour). The fact that it must have a colour is simply a feature of my car domain model which states that I cannot construct a car instance without supplying the colour.
当涉及聚合根设计注意事项时,我经常引用此资源
RavenDB - StackOverflow 风格的 Live 投票预测。这解决了您的一些关键思维过程,并且应该向您展示为什么类别将是一个非常差的聚合根。
I often cite this resource when it comes to aggregate root design considerations
RavenDB - StackOverflow style voting with Live Projections. This addresses some of your key thought process and should show you why Category would be a VERY POOR aggregate root.