我们可以在没有 CQRS 的情况下使用事件驱动吗
我在事件驱动方面的经验还不到一年,但我不太明白为什么我们需要它。正如我的小大脑所记得的,CQRS 帮助我们分离命令模型(写入)/查询模型(读取),以便我们可以轻松地设计用于写入和读取的数据库。听起来不错,但它在写入和读取时间线之间存在一些一致性问题,或者也称为基于集合的一致性,我如何确保时间线之间是否没有错误,或者如果发生错误,我什至如何确保当前的读取模型有效吗?所以我开始问自己是否真的需要 CQRS。我不能只使用事件驱动而不使用它并专注于事件的存储方式吗?所以我想出了这样的事情。
public class UserController {
@PostMapping
public CompletableFuture<String> createUser(request: RegisterUserRequestDTO){
// some validation and business logic here
// publish event if succeeded
publisher.publish(UserRegisteredEvent.create(request));
}
}
class UserEventHandler {
public void handle(event: UserRegisteredEvent){
// save data to the db here or the read model
}
}
所以我还没有尝试过,但它似乎可能有效?所以我是一个愚蠢的人,经验较少,所以如果我错了,请纠正我。为什么我们需要CQRS与事件驱动相结合?我可以在没有 CQRS 的情况下使用事件驱动吗?其背后的想法是什么。
I have experience with Event-driven for less than 1 year and there's something that I don't really understand that why do we need it. As my tiny brain can remember, CQRS help us separate command model (write) / query model (read) so we can easily design database for write and read. It sounds good, but it has some consistency problem between write and read timeline or also known as set based consistency and how do I make sure if there's no error between the timeline or if it occurs how do I even make sure if the current read model is valid?. So I start asking myself do I really need CQRS. Can't I just use Event-driven without it and focusing on how the event are stored. So I came up with something like this.
public class UserController {
@PostMapping
public CompletableFuture<String> createUser(request: RegisterUserRequestDTO){
// some validation and business logic here
// publish event if succeeded
publisher.publish(UserRegisteredEvent.create(request));
}
}
class UserEventHandler {
public void handle(event: UserRegisteredEvent){
// save data to the db here or the read model
}
}
So I haven't tried it yet, but it seems to work maybe? So I'm a stupid guy with less experience anyway so correct me if i'm wrong. why do we need CQRS combine with Event-driven? Can I use Event-driven without CQRS? What's the idea behind it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CQRS 和事件驱动是两种完全不同的模式,可以非常成功地单独应用和使用。
但和DDD一起非常契合!
访问 CQRS.NU ,您可以在其中找到一种实现方式看看 在。
CQRS and event-driven are two totally different patterns that can very successfully be applied and used on its own.
But together with DDD the fit very well together!
Visit CQRS.NU where you can find one implementation of this that you can take a look at.