简体   繁体   English

如何从客户端角度处理CQRS

[英]How to handle CQRS from a client-side perspective

My company is planning to use CQRS architecture on our back-end but as a client-side developer I'm a bit confused about how to consume a request. 我的公司计划在后端使用CQRS架构,但作为客户端开发人员,我对如何使用请求感到困惑。 Here are the methods that I came up with, none being ideal from my point of view: 以下是我提出的方法,从我的观点来看,没有一个是理想的:

  1. The server waits until the queue gets processed and gives back the needed data in the response. 服务器等待,直到队列得到处理,并在响应中返回所需的数据。 (sounds like a pretty poor approach from a performance point of view); (从性能的角度来看,这听起来很糟糕);
  2. The client makes the request and gets back a "202 Accepted" once the request was added to the queue, than the client uses an interval system to pool until it gets the necessary data from the back-end (I'm not a big fan of doing X http request every second); 一旦请求被添加到队列,客户端发出请求并返回“202 Accepted”,而客户端使用间隔系统进行池化,直到从后端获取必要的数据(我不是一个大粉丝)每秒做一次X http请求);
  3. Web Sockets ... seems like the perfect solution as the server would be able to push the data to the client once it was processed (I'm a bit concerned about having thousands of sockets opened around the entire app). Web套接字......似乎是完美的解决方案,因为服务器能够在处理后将数据推送到客户端(我有点担心在整个应用程序周围打开数千个套接字)。

So the question is: What is the best approach to this problem? 所以问题是:解决这个问题的最佳方法是什么? (not necessarily one of the above) (不一定是上述之一)

Well, it all depends on what you plan to do in the UI. 嗯,这一切都取决于你打算在UI中做什么。

If you have a SPA and good understanding of events and subscriptions in Javascript , a single WebSocket connection seems like the most natural option. 如果你有一个SPA并且对Javascript中事件和订阅有很好的理解,那么单个WebSocket连接似乎是最自然的选择。 Thousands of WebSocket connections are not a problem in the server if the connections are idle most of the time, and the server is asynchronous (to avoid thread starvation). 如果连接在大多数时间处于空闲状态,并且服务器是异步的(以避免线程不足),则数以千计的WebSocket连接在服务器中不是问题。 But then... why do you want a REST interface? 但那么......为什么你想要一个REST接口? you can send requests through the WebSocket with a correlation ID and then wait for a response with that same correlation id to know when it is done, this way you would avoid the overhead of creating new connections per each request. 您可以通过带有相关ID的WebSocket发送请求,然后等待具有相同相关ID的响应以了解何时完成,这样可以避免每个请求创建新连接的开销。

If you are more comfortable with AJAX and want to use REST, then you can use this asynchronous approach , but you need to pool till you get the HTTP 303 See Other . 如果您对AJAX更熟悉并且想要使用REST,那么您可以使用这种异步方法 ,但是您需要进行池化,直到获得HTTP 303 See Other It is not perfect, but it is probably better than opening a WebSocket only for the sake of that operation. 它并不完美,但它可能只是为了操作而打开WebSocket。

Blocking the connection till there is a response is a very bad approach. 阻止连接直到有响应是一种非常糟糕的方法。 The network cannot be considered reliable, so if the connection cuts, the browser needs to know how to find out the result o fthe operation. 网络不能被认为是可靠的,因此如果连接断开,浏览器需要知道如何找出操作的结果。

To complete vtortola's answer, you can also check this project SwaggerSocket . 要完成vtortola的答案,您还可以检查此项目SwaggerSocket It aims to provide a websocket interface to a REST API for better performances. 它旨在为REST API提供websocket接口以获得更好的性能。

Transport is not the only problem, how will you sync the contracts on server and client? 传输不是唯一的问题,您将如何同步服务器和客户端上的合同? I solved this with rendering all my Commands and Queries into a javascript. 我通过将所有命令和查询呈现为javascript来解决这个问题。 In my case I used t4 template engine but any template engine can be used. 在我的情况下,我使用t4模板引擎,但可以使用任何模板引擎。 You can read more here 你可以在这里阅读更多

http://andersmalmgren.com/2014/02/05/typed-javascript-contracts-using-t4-templates/ http://andersmalmgren.com/2014/02/05/typed-javascript-contracts-using-t4-templates/

As for transport I think REST is fine, if you use for example .NET WebApi and uses the async keyword correctly the framework will reuse the thread when ever there are I/O that is waiting (DB etc) 至于传输,我认为REST很好,如果你使用例如.NET WebApi并正确使用async关键字,那么当有等待的I / O时,框架将重用该线程(DB等)

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

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