CQRS, Task Based UIs, Event Sourcing agh!
原文地址:CQRS, Task Based UIs, Event Sourcing agh!
Many people have been getting confused over what CQRS is. They look at CQRS as being an architecture; it is not. CQRS is a very simple pattern that enables many opportunities for architecture that may otherwise not exist. CQRS is not eventual consistency, it is not eventing, it is not messaging, it is not having separated models for reading and writing, nor is it using event sourcing. I want to take a few paragraphs to describe first exactly what CQRS is and then how it relates to other patterns.
許多人已經搞不清是什么CQRS。他們看cqrs作為架構,但是它不是。CQRS是一個非常簡單的模式,使得很多的機會可能不存在的架構。CQRS不是最終一致性,它不是事件,它不是消息,它不具有讀寫分離的模式,也不是使用事件源。我想把幾段描述什么是第一CQRS然后如何涉及到其他模式。
CQRS Command and Query Responsibility Segregation CQRS命令和查詢職責分離
Starting with CQRS, CQRS is simply the creation of two objects where there was previously only one. The separation occurs based upon whether the methods are a command or a query (the same definition that is used by Meyer in Command and Query Separation, a command is any method that mutates state and a query is any method that returns a value).
從CQRS,CQRS僅僅是兩個對象那里以前只有一個創作。根據是否發生分離的方法是命令或查詢(相同的定義是由Meyer在命令和查詢,命令任何方法改變狀態和查詢使用的是任何返回值的方法)。
When most people talk about CQRS they are really speaking about applying the CQRS pattern to the object that represents the service boundary of the application. Consider the following pseudo-code service definition.
當大多數人談論cqrs他們確實說的是關于應用CQRS模式的對象,代表應用程序的服務邊界對象。考慮下面的偽代碼服務定義。
CustomerService 客戶服務
void MakeCustomerPreferred(CustomerId)
Customer GetCustomer(CustomerId)
CustomerSet GetCustomersWithName(Name)
CustomerSet GetPreferredCustomers()
void ChangeCustomerLocale(CustomerId, NewLocale)
void CreateCustomer(Customer)
void EditCustomerDetails(CustomerDetails)
Applying CQRS on this would result in two services 應用CQRS這將導致兩個服務
CustomerWriteService
void MakeCustomerPreferred(CustomerId)
void ChangeCustomerLocale(CustomerId, NewLocale)
void CreateCustomer(Customer)
void EditCustomerDetails(CustomerDetails)
CustomerReadService
Customer GetCustomer(CustomerId)
CustomerSet GetCustomersWithName(Name)
CustomerSet GetPreferredCustomers()
That is it. That is the entirety of the CQRS pattern. There is nothing more to it than that… Doesn’t seem nearly as interesting when we explain it this way does it? This separation however enables us to do many interesting things architecturally, the largest is that it forces a break of the mental retardation that because the two use the same data they should also use the same data model.
這就是,cqrs模式的整體性。沒有什么比這更有趣的了,當我們解釋它的時候,似乎沒有什么有趣的事嗎?這種分離而使我們的建筑做很多有趣的事情,最大的是它強制分離,因為兩使用也應該使用相同的數據模型相同的數據。
The largest possible benefit though is that it recognizes that their are different architectural properties when dealing with commands and queries … for example it allows us to host the two services differently eg: we can host the read service on 25 servers and the write service on two. The processing of commands and queries is fundamentally asymmetrical, and scaling the services symmetrically does not make a lot of sense.
最大可能的好處是,在處理命令和查詢時可具有不同的架構特性,例如它允許我們host這兩項不同的服務:我們可以在25個服務器上進行讀寫服務,并在2個服務器上進行寫服務。命令和查詢的處理是從根本上是不對稱的,縮放服務對稱的不做很多的意義。
Task Based UI 基于任務的用戶界面
A task based UI is quite different from a CRUD based UI. In a task based UI you track what the user is doing and you push forward commands representing the intent of the user. I would like to state once and for all that CQRS does not require a task based UI. We could apply CQRS to a CRUD based interface (though things like creating separated data models would be much harder).
基于任務的用戶界面是一個CRUD為基礎的用戶界面完全不同。在任務為基礎的用戶界面中,您可以跟蹤用戶正在做的事情,然后向用戶推送命令,表示用戶的意圖。我想一勞永逸,CQRS并不需要一個基于任務的用戶界面。我們可以運用CQRS到CRUD界面(盡管諸如創建獨立的數據模型會更難)。
There is however one thing that does really require a task based UI… That is Domain Driven Design.
然而有一件事需要一個基于任務的用戶界面……這是域驅動設計。
The Application Service Layer in Domain Driven Design represents the tasks the system can perform. It does not just copy data to domain objects and save them… It should be dealing with behaviors on the objects… Before going further let’s look at what happened if we did; there would be no verbs in our ubiquitous language except for “Create”, “Delete”, and “Change”.? While there exist many domains where this is what the Ubiquitous Language is actually like, you probably should not be using Domain Driven Design for such systems.
應用服務層在域驅動設計中表示系統可以執行的任務。它不只是復制數據到域對象,并保存它們……它應該處理的對象上的行為……然后再讓我們看看發生了什么,如果我們做了,我們無處不在的語言,除了“創建”,“刪除”,和“變化”。雖然存在許多領域,這是無處不在的語言實際上是一樣的,你可能不應該使用域驅動的設計,這樣的系統。
The concept of a task based UI is more often than not assumed to be part of CQRS, it is not, it is there so the domain can have verbs but also capturing the intent of the user is important in general. Was this a managerial override or a normal update? Does it make a difference? It depends on what question you want to ask …
一個基于用戶界面為基礎的任務的概念的往往不認為是CQRS的一部分,它不是,它是那么的域可以有動詞也捕捉用戶的意圖,總的來說是重要的。這是一個管理覆蓋或正常更新嗎?它有區別嗎?這取決于你想問的是什么問題…
Moving on to the next pattern that gets confused into CQRS
Event Sourcing 事件源
For this I want to be clear, when I use this term I am not encompassing all of what is written on the bliki. I am referring to storing current state as a series of events and rebuilding state within the system by replaying that series of events.
這個要想清楚,當我用這個詞我不包括所有寫在bliki是什么。我指的是存儲當前狀態的一系列事件和重建狀態在系統內的這一系列事件的回放。
On the command side of the equation, since reads are no longer on the domain, storing events can be a great way of keeping the current state. The value increases more if you decide to have two separate models (a write model and a read model) and you have a need to integrate between the two of them as you will likely be doing that through events. Since you are building the eventing anyway, why not just use the one model to manage your state?
在命令方式這一邊上,因為讀取不再是在域上,存儲事件可以是一個偉大的方式來保持當前狀態。如果你決定要有2個獨立的模型(一個寫模型和一個讀模型),你就有了一個需要在兩者之間進行整合,因為你很可能會通過事件來做這些事情。既然你要構建事件,為什么不只是使用一個模型來管理狀態?
Messaging Patterns 消息模式
There is no need to use messaging patterns with CQRS. That said, if you separate your data models you will more likely than not use messaging in the integration between the models because it offers interesting possibilities.
沒有必要使用消息模式與CQRS。也就是說,如果你將你的數據模型分離,你將更可能比不使用消息傳遞的模型,因為它提供了有趣的可能性。
Finally I come to the last “pattern” I hate to call it a pattern when it is really a concept that people tend to put into their definitions of CQRS and it goes hand in hand with messaging.
最后我到了最后的“模式”,我討厭把它稱為一個模式時,這是一個真正的概念,人們往往更傾向于定義為CQRS,并消息齊頭并進。
Eventual Consistency 最終一致性
Eventual consistency is also quite often introduced between the services. It is done for many architectural reasons but the largest is that it allows you to increase your scalability and availability. If you remember CAP theorem consistency if given up allows increases in the other two. Eventual consistency is extremely useful in between the models if you have them in a separated fashion but is in no way a property of CQRS itself.
最終的一致性也很經常被介紹的在兩個服務之間。它是服務于許多架構的原因,但最大的理由是,它允許您增加可擴展性和可用性。如果你還記得CAP定理的一致性,如果放棄允許增加其他兩個。最終一致性是在模型之間是非常有用的,如果你在分離方面使用他們,但絕不是CQRS本身屬性。
Going through all of these we can see that CQRS itself is actually a fairly trivial pattern. What is interesting around CQRS is not CQRS itself but the architectural properties in the integration of the two services. In other words the interesting stuff is not really the CQRS pattern itself but in the architectural decisions that can be made around it. Don’t get me wrong there are a lot of interesting decisions that can be made around a system that has had CQRS applied … just don’t confuse all of those architectural decisions with CQRS itself.
通過這些我們可以看到,CQRS本身其實是一個相當普通的模式。有趣的是在CQRS不CQRS本身,但在這兩個服務集成架構的特性。換句話說,有趣的東西是不是真的CQRS模式本身,可它周圍的建筑的決定。不要誤會我有很多有趣的決定,可以在一個系統,有CQRS應用…只是不要把所有這些建筑的決定cqrs本身。
On a personal note it feels so weird to be writing to a blog again. Writing for a book is hard, writing for a blog is easy. Pages in the book average me an hour or more while this took me just over 30 minutes to write. The less formal style allows one to write in a more stream of consciousness manner. Anyways, hope everyone enjoys the blog posts that will be pouring in over the next short period.
在個人筆記上,寫博客感覺很奇怪。寫一本書很難,寫博客很容易。這本書平均每小時我一個小時或更多,而這只花了我30分鐘的時間寫。不太正式的風格讓人在意識的方式上寫下更多的意識。不管怎樣,希望大家都喜歡在下一個短時期內就可以投入的博客文章。
轉載于:https://www.cnblogs.com/zsy/p/5185198.html
總結
以上是生活随笔為你收集整理的CQRS, Task Based UIs, Event Sourcing agh!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SVM: 实际中使用SVM的一些问题
- 下一篇: 入住cnblogs第一篇随笔 Hello