正确的聚合根是什么?

发布于 2024-12-15 03:36:18 字数 440 浏览 1 评论 0原文

我有以下数据结构:

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 as GetCategory and GetPost) but not a PostRepository (because Post is not an aggregate root)

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

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

发布评论

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

评论(2

岛歌少女 2024-12-22 03:36:18

您想如何引用 Post 的实例? Post.TitlePost 的唯一标识符吗?如果是这样,那么 Post 是一个有效的聚合根,您应该创建一个 PostRepository 来检索 Post 的实例给定它的标题

以汽车为例。汽车必须有颜色,但仅仅因为汽车没有颜色就无法存在,就说颜色是聚合根,这是错误的做法。我想根据汽车的车牌号(与其颜色无关)独立地引用一辆汽车。它必须有颜色这一事实只是我的汽车域模型的一个特征,它表明我无法在不提供颜色的情况下构建汽车实例。

How do you want to reference instances of Post? Is Post.Title a unique identifier for a Post? If so, then Post is a valid aggregate root and you should create a PostRepository which retrieves an instance of Post given it's Title.

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.

三寸金莲 2024-12-22 03:36:18

当涉及聚合根设计注意事项时,我经常引用此资源
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.

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