CQRS:如何检索有关已执行命令的信息?
在我见过的大多数命令界面中,通常有一个“执行”方法,它接受命令输入,然后返回 void 或返回一些通用结构,指示命令是否成功执行(我们正在使用后者)。现在,我以前从未想过这一点,但我们突然需要了解有关命令结果的更多详细信息,而不是一般可以公开的信息。
考虑以下示例:
您有一个团队,并且正在创建一个屏幕,您可以在其中向团队添加成员。团队成员显示在“添加新成员”下方的网格中。现在,当您按“添加新成员”时,您想要运行一些 jquery/roundohuse/whatever 并将新成员添加到团队成员列表中。到目前为止没有问题,但是:您还希望在每个成员的隐藏字段中包含一些标识数据,并且此 id 数据来自服务器。
所以问题是:如何从服务器获取该 id 数据?我通过“ExecuteCommand”方法推送的“AddNewTeamMember”命令不会给我任何有用的信息,如果我向服务添加一个新的查询方法,例如:“GetLastAddedTeamMember”,那么我可能只会得到最后一个条目由其他人添加(至少如果这是由不同用户非常积极地添加的数据)。在某些情况下,您可以在客户端生成我们可以使用的自然唯一标识符,但对于团队成员,我们没有。
In most command interfaces I've seen, there is typically an "Execute" method which takes takes a command input and either returns void or some generic structure indicating if the command executed successfully or not (we are using the latter). Now, I've never thought of this before, but we suddenly got the need to know some more details about the result of the command than what you can expose generically.
Consider the following example:
you have a team and you are creating a screen where you can add members to your team. The members of the team are shown in a grid below the "add new member"-stuff. Now, when you press "add new member" you want to run some jquery/roundohuse/whatever and add the new member to the list of team members. No problems so far, but: you also want to include some identification data in a hidden field for each member and this id-data comes from the server.
So the problem is: how can I get that id-data from the server? The "AddNewTeamMember" command which I am pushing through the "ExecuteCommand"-method does not give me anything useful back, and if I add a new query method to the service saying something like: "GetLastAddedTeamMember" then I might just get the last entry added by someone else (at least if this is data which is very aggressively added by different users). In some situations you have a natural unique identifier generated on the client side which we can use, but for team members we did not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
鉴于您别无选择,只能在另一个命令完成时更新页面小部件,我认为您有两种选择:
关闭该命令,在本地显示一些表明已提交的内容,然后等待,直到您收到来自服务器的通知,表明团队成员列表已更改。更新小部件以反映这一点。
在提交命令时将相关 ID 添加到命令中,并将团队成员临时添加到本地列表中。当您从服务器确认由于您的相关 ID 导致团队成员更新时,请更新您的本地数据。
我建议采用第一种方法,其中“临时指标”可以将正常指示的标记版本投入到位;然后,当您最终获得更新时,您应该拥有所需的数据。
鉴于您使用 CQRS 来解决这个问题,我假设您已经在后台频繁更新这些小部件的内容,因此大概已经解决了“后台更新”问题。
如果没有,我建议您要么放弃 CQRS 作为您的问题空间中糟糕的(过于复杂的)解决方案,要么首先解决后台更新问题。
Given that you have no choice but to update an on-page widget when another command completes, I see two choices for you:
Shoot off the command, display something locally that indicates it is submitted, and then wait until you get a notification from the server that the team member list has changed. Update the widget to reflect that.
Add a correlation ID to your command when you submit it, and add the team member provisionally locally to the list. When you get a confirmation from the server that a team member update happened because of your correlation ID, update your local data.
I would suggest the first approach, where the "provisional indicator" could be throwing a marked version of the normal indication into place; then, when you finally get an update you should have the data you need.
Given you went with CQRS to solve this problem I assume you have frequent updates to the content of those widgets happening in the background already, so have presumably solved the "background update" problem.
If not, I suggest you either ditch CQRS as a bad - over-complicated - solution in your problem space, or solve the background update problem first.
如果您想添加现有团队成员,您应该查询应用程序的读取端以获取此数据。如果您需要添加新的团队成员,您必须考虑是否有必要立即在下面的网格中显示该用户。您可以等到团队成员在读取端就位吗?您还可以查询服务器端的服务以获取唯一的 ID(它可以返回 GUID)。然后,您将团队成员添加到网格中,当然,还将命令发送到服务器。但是,如果可能的话,请尝试以不必立即向团队成员展示的方式设计应用程序。还可以向用户发送一条消息,内容如下:“团队成员已添加,正在等待服务器响应。”。然后使用 AJAX 查询读取端是否有新的团队成员。当它出现在读取面时,将其显示在网格中。您可能需要处理其他用户添加的团队成员,但这有关系吗? CQRS 为您提供了一种与其他用户协作的好方法,因此也许您应该利用它。据我看来; CQRS 迫使您以不同的方式思考,这可能不是一件坏事。
If you want to add an existing team member, you should query the read side of your application for this data. If you need to add a new team member, you have to consider if it's necessary to show the user in the grid below at once. Can you wait until the team member is in place on the read side? You can also query a service on the server side to get an unique ID (it can return a GUID). Then you add the team member to the grid, and of course, send the command to the server. But, if it's possible, try to design the application in a way that you don't have to show the team member at once. It's also possible to give the user a message saying something like this: "Team member added, waiting for response from server.". Then use AJAX to query the read side for new team members. When it appears on the read side, show it in the grid. You might have to deal with team members added by other users, but does it matter? CQRS gives you a great way to collaborate with other users, so maybe you should take advantage of that. As I see it; CQRS forces you to think different, and that may not be a bad thing.