【问题标题】:Event Sourcing and cqrs with eventstore db事件溯源和 cqrs 与 eventstore db
【发布时间】:2021-03-13 14:50:45
【问题描述】:

存储数据库和事件源,但我对预测和 cqrs 有疑问。 到目前为止,这是我调用突击队和命令处理程序的方式:

创建用户命令

export class CreateUserCommand implements ICommand {
  constructor(
    public readonly userDto: UserStruct,
  ) {}
}

命令处理程序:

export class CreateUserHandler implements ICommandHandler<CreateUserCommand> {
  constructor(private readonly publisher: EventPublisher) {}

  async execute(command: CreateUserCommand) {
    const { userDto } = command;
    const user = User.create(userDto);
        console.log(user.value)
    if (user.isLeft()) throw user.value;
    const userPublisher = this.publisher.mergeObjectContext(user.value);
        userPublisher.commit()
  }
}

事件:

export class UserCreatedEvent implements IEvent {
  static readonly NAME = "UniFtcIdade/user-registered";
  readonly $name = UserCreatedEvent.NAME;
  readonly $version = 0;
  constructor(
    public readonly aggregateId: string,
    public readonly state: { email: string; name: string },
    public readonly date: Date
  ) {
  }
}

域:

export class User extends AggregateRoot {
  public readonly name: string;
  public readonly email: string;

  private constructor (guid: string, name: string, email: string) {
    super()
    this.apply(new UserCreatedEvent(guid, {email, name}, new Date()));
  }
  static create(
    dto: UserStruct
  ): Either<InvalidNameError | InvalidEmailError, User> {
    const name: Either<InvalidNameError, Name> = Name.create(dto.name);
    const email: Either<InvalidEmailError, Email> = Email.create(dto.email);
    if (name.isLeft()) return left(name.value);
    if (email.isLeft()) return left(email.value);
    const user = new User(v4(),name.value.value, email.value.value);
    return right(user);
  }
}

但我怀疑预测如何进入这种情况。 投影用于获取聚合的当前状态??? 我应该有一个 db 作为 mongodb 来保存当前状态,也就是说,每次我调用我的命令处理程序并更改 mongodb 中的当前状态? eventstoredb的投影是为了这个吗?保存聚合的当前状态??

【问题讨论】:

    标签: typescript event-sourcing eventstoredb


    【解决方案1】:

    在 CQRS 中,使用 EventStoreDb 时,您的聚合必须设计为从事件恢复到状态。事件存储在具有唯一名称和标识符 (guid) 的流中。修改聚合时,您必须读取此流,并按顺序应用每个事件以恢复当前状态,然后再对聚合执行任何更改(这会生成更多事件)。为了保持完整性和处理乐观并发,您应该在聚合中进行简单的版本检查,计算旧事件 + 新事件以确定要持久化的最新版本号。

    我在上面看到的问题如下。 你的聚合有一个构造函数和一个静态方法,它在没有对当前状态进行任何验证的情况下生成事件,即:如果我用相同的 guid 调用 create 两次会发生什么?

    this.apply(new UserCreatedEvent(guid, {email, name}, new Date()));

    您在此处直接应用状态。相反,您应该在 Create 方法中引发事件。

    this.raiseEvent(new UserCreatedEvent(guid, {email, name}, new Date()));

    应该执行以下操作。

    • 已添加到未提交事件列表中
    • this.apply 调用

    然后,您应该将事件保存到命令处理程序中的 EventStoreDb。

    async execute(command: CreateUserCommand) {
        const { userDto } = command;
        const user = eventRepository.Get<User>(command.Id);
        user.Create(userDto); // Can now check current state and fail if required.
        eventRepository.Save(user)
      }
    

    这里的存储库很简单。它可以创建一个空用户并在返回用户之前按顺序应用所有事件。 保存应该只是读取未提交事件的列表并将它们保存到用户流。

    这就是命令端完成的, 对于读取端,您可以使用所有用户的开箱即用类别投影,并将它们写入 mongo 以供不同的 API(而不是您的命令处理程序)读取。

    【讨论】:

    • 谢谢 我从我的静态方法中注意到了这一点,你能再问我一个问题吗?我正在尝试应用具有这种 cqrs 和事件源结构的微服务结构,但我有疑问,例如,我的命令已创建,在异步场景中我必须发布关于我的主题的消息,并监听我的服务或控制器,当我得到答案时,我会打电话给我的命令处理程序吗?还是我在我的命令处理程序中完成所有这些?换句话说,我需要来自其他微服务的答案才能继续执行我的命令处理程序。
    • 你的微服务应该有一个 API,你可以在创建命令并在总线上提供它之前读取它,你的命令处理程序不需要从另一个微服务中读取。请注意,您可能使用的是陈旧数据,但这是另一个问题。
    猜你喜欢
    • 2019-01-31
    • 2016-02-14
    • 2018-11-15
    • 2019-09-22
    • 2018-04-13
    • 1970-01-01
    • 2021-12-18
    • 2016-02-20
    • 1970-01-01
    相关资源
    最近更新 更多