简体   繁体   English

为什么 DDD 用于 CQRS 和事件溯源应用程序?

[英]Why DDD for CQRS and Event Sourcing application?

I am just starting with the CQRS, Event Sourcing.我刚开始使用 CQRS,即事件溯源。 While examining many frameworks, I have realized that one of the thing most of the frameworks assumed that our application is modelled based on the DDD principles.在研究许多框架时,我意识到大多数框架假设我们的应用程序是基于 DDD 原则建模的一件事。 I am not sure but can anyone explain to me that how DDD, CQRS, and Event Sourcing are related to each other and why DDD is generally required for implementing the CQRS and Event Sourcing pattern?我不确定,但谁能向我解释 DDD、CQRS 和事件溯源如何相互关联,以及为什么通常需要 DDD 来实现 CQRS 和事件溯源模式?

why DDD is generally required for implementing the CQRS and Event Sourcing pattern?为什么通常需要 DDD 来实现 CQRS 和事件溯源模式?

Tradition.传统。

Before being rebranded as "CQRS", the same collection of ideas wore the label "Distributed Domain Driven Design".在更名为“CQRS”之前,相同的创意集合使用了 label“分布式域驱动设计”。 Greg Young's early presentations were experience reports describing how he adapted the ideas published by Evans when designing an algorithmic trading system. Greg Young 早期的演讲是经验报告,描述了他在设计算法交易系统时如何适应 Evans 发表的想法。

In effect, this means that most of the audience that discovers CQRS and Event Sourcing have already been convinced that Domain Driven Design is a good idea.实际上,这意味着发现 CQRS 和事件溯源的大多数观众已经相信领域驱动设计是一个好主意。

They are different ideas, though;不过,它们是不同的想法。 the CQRS pattern might be useful even when you aren't doing event sourcing.即使您不进行事件溯源,CQRS 模式也可能很有用。

can anyone explain to me that how DDD, CQRS, and Event Sourcing are related to each other谁能向我解释 DDD、CQRS 和事件溯源是如何相互关联的

Event sourcing is really a pair of ideas事件溯源真的是一对思路

  • changes should be first class citizens in our model更改应该是我们 model 中的第一个 class 公民
  • our authoritative data model should be a sequence of changes (aka a history)我们的权威数据 model 应该是一系列变化(又名历史)

This kind of data model is great for writes (just append a new change on the end of the list), and has the additional benefit that edits are non-destructive;这种数据 model 非常适合写入(仅 append 是列表末尾的新更改),并且具有编辑无损的额外好处; all of the information added to the system is still there, so you can easily time machine back to understand what things looked like prior to some change.添加到系统中的所有信息仍然存在,因此您可以轻松地时光机回溯以了解发生某些变化之前的情况。

But histories suck for low latency query.但是历史记录很适合低延迟查询。 Therefore: caches - we'll pre-compute the answers to some questions, and stick them in a property bag.因此:缓存——我们将预先计算一些问题的答案,并将它们放入属性包中。

Now we have two models: the history, and the cache.现在我们有两个模型:历史和缓存。 And what is CQRS?什么是 CQRS?

CQRS is simply the creation of two objects where there was previously only one -- Greg Young 2010 CQRS 只是创建两个对象,而以前只有一个对象——Greg Young 2010

I don't think it's only tradition .我不认为这只是传统 When designing a system that would be using events as state transitions, the most useful approach is getting away from CRUD-style events (SomethingCreated, SomethingUpdated, etc), and model the actual behaviour instead.在设计将事件用作 state 转换的系统时,最有用的方法是摆脱 CRUD 样式的事件(SomethingCreated、SomethingUpdated 等),而改为使用实际行为 model。 That's where Event Sourcing comes very close to the foundational idea of DDD - rich Domain Models, expressed with Ubiquitous Language.这就是事件溯源非常接近 DDD 的基本思想的地方——丰富的领域模型,用通用语言表达。

Also, when we design an event-sourced system, we put some effort into defining stream boundaries, so we don't get large transactions, we don't have multi-stream transactions, the average stream length is upper-bound, etc. All these concerns align very nicely with the tactical Aggregate pattern of DDD.另外,当我们设计一个事件源系统时,我们花了一些精力来定义 stream 边界,所以我们没有得到大事务,我们没有多流事务,平均 stream 长度是上限等。所有这些问题都与 DDD 的战术聚合模式非常吻合。

When people just shovel events to Kafka topics and project them to some databases, they essentially are not doing Event Sourcing, but implementing an event-driven system instead.当人们只是将事件铲到 Kafka 主题并将它们投影到某些数据库时,他们本质上不是在做事件溯源,而是在实现一个事件驱动的系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM