我的活动是否以正确的聚合方式提出

发布于 2025-01-14 07:52:04 字数 1431 浏览 1 评论 0原文

我对实现 DDD + CQRS + 事件溯源系统还很陌生。以下是我正在建模的业务环境:

用户可以创建组织。一个组织可以有 多个成员会员可以是普通会员或 管理员成员。默认创建组织的用户 第一个管理员

特别是,我试图表示在域丰富的实现中创建组织的行为。以下是我到目前为止所拥有的。

class User extends Aggregate {
  constructor(
    public readonly id: UserId,
    public readonly name: Name,
    public readonly email: Email
  ) {
    super(id);
  }

  createOrganization(orgId: OrgId, name: Name) {
    const creator = new Member(orgId, MemberRoles.Admin, this.id);
    return new Organization(orgId, name, [creator]);
  }
}

class Organization extends Aggregate {
  public readonly members: Member[];
  public readonly name: Name;

  constructor(
    id: OrgId,
    name: Name,
    members: Member[],
  ) {
    super(id);
    this.name = name;
    this.members = members
  }

  addMember(member) { /**/}
  removeMember(memberId) { /**/}
}

我喜欢 User 类上有一个 .createOrganization() 方法,因为它说明了用户有能力这样做。但是我应该在哪里引发UserCreatedOrganizationEvent?我应该在 User 类中执行它吗?在一个聚合(User)中创建事件,然后将其用于直接水合另一聚合(Organization),这不是错误的吗?我一直听说每个聚合都应该从自己的事件中吸收自身,以确保始终保证不变量。那么在组织中使用静态 create 方法是否更有意义?但我无法从 static 方法引发与我的聚合关联的事件。任何对此的想法将不胜感激。

I'm pretty new to implementing a DDD + CQRS + event sourcing system. The following is the business context I'm modelling:

A User can create an Organization. An Organization can have
multiple Members. Members can either be Regular members or
Admin members. The user that creates the organization is by default
the first Admin.

In particular, im trying to represent the act of creating an organization in an implementation that is domain rich. The following is what I have so far.

class User extends Aggregate {
  constructor(
    public readonly id: UserId,
    public readonly name: Name,
    public readonly email: Email
  ) {
    super(id);
  }

  createOrganization(orgId: OrgId, name: Name) {
    const creator = new Member(orgId, MemberRoles.Admin, this.id);
    return new Organization(orgId, name, [creator]);
  }
}

class Organization extends Aggregate {
  public readonly members: Member[];
  public readonly name: Name;

  constructor(
    id: OrgId,
    name: Name,
    members: Member[],
  ) {
    super(id);
    this.name = name;
    this.members = members
  }

  addMember(member) { /**/}
  removeMember(memberId) { /**/}
}

I like the fact that I have a .createOrganization() method on the User class since it illustrates the fact that a user has the ability to do so. But where should I raise UserCreatedOrganizationEvent? Should I do it in the User class? Is it not wrong to create events in one aggregate (User) which would then be used to directly hydrated another aggregate (Organization)? I keep hearing that each aggregate should hydrate themselves from their own events to ensure invariants are always ensured. Then does it make more sense to have a static create method on the organization? But I can't raise an event associated to my aggregate from a static method. Any thoughts on this would be appreciate.

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

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

发布评论

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

评论(1

怼怹恏 2025-01-21 07:52:04

创建组织聚合作为用户聚合的一部分是错误的,因为用户聚合最终会理解组织聚合的复杂性。他们最终紧密耦合。

管理员用户只是创建组织的众多细节之一。我将其视为身份验证/授权问题,它将验证是否应允许执行该操作的用户执行该操作。

是的,通常,您会在组织聚合中创建一个工厂方法(类方法),该方法将实例化聚合并在实例化的聚合对象上引发一个事件。工厂方法将由命令处理程序调用,处理创建组织的命令。

考虑相反的论点,以清楚地理解为什么用户通过用户聚合创建组织是不可持续的。从技术上讲,应用程序中的每一个操作都将由用户执行。这并不意味着用户聚合负责系统中的所有其他聚合。

Creating the Organization aggregate as part of your User aggregate is wrong because the User aggregate ends up understanding the intricacies of the Organization aggregate. They end up being tightly coupled.

The Admin user is just one of the many details that go into creating the organization. I would think of it as an Authentication/Authorization concern that would validate if the user performing the action should be allowed to go through it.

And yes, typically, you would create a factory method (a class method) within the Organization aggregate that would instantiate the Aggregate and raise an event on the instantiated aggregate object. The factory method would be invoked by a Command Handler, handling the command to create an organization.

Think of the contra argument to clearly understand why the user creating the organization via the user aggregate is not sustainable. Technically, every single action in your application would be performed by a user. That does not mean that the user aggregate is responsible for all other Aggregates in the system.

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