【发布时间】: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