来自外部系统的事件源命令或事件? [英] Event Sourcing command or event from external system?

查看:68
本文介绍了来自外部系统的事件源命令或事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数情况下,我了解CQRS + ES系统中命令和事件之间的区别。但是,有一种情况我无法弄清。

In most cases I understand the distinction between a command and an event in a CQRS + ES system. However, there is one situation that I can't figure out.

假设我正在建立一个个人财务跟踪系统,用户可以在其中输入借方/贷方。显然,这些是命令,并且一旦它们被验证,域模型就会被更新并发布事件。但是,假设贷方/借方信息也直接来自外部系统,例如该用户的花店发送一条消息,表示他已为自己的母亲节重复递送费用从该用户的信用卡中扣款。在这种情况下,该消息似乎是一个事件(已发生收费),但是该消息可能格式错误,将被拒绝。那真的是命令吗?但是然后需要一种发送ACK / NACK的方法,在这里不是这种情况(花店只有在知道借记发生后才发送消息)。

Suppose I am building a personal finance tracking system, where a user can enter debits/credits. Clearly these are commands, and once they are validated the domain model gets updated and an event is published. However, suppose that credit/debit information also comes directly from external systems e.g. the user's florist sends a message that he has charged the user's credit card for his recurring Mother's Day delivery. In this case the message seems like it is an event (the charge has already occurred), but it is possible that the message is malformed and will be rejected. So is it really a command? But then there needs be a way to send an ACK/NACK, which isn't the case here (the florist only sends the message once he knows that the debit has taken place).

编辑:只是为了澄清,我并不是说外部系统对我的事件和/或命令一无所知。我有一个组件,它从外部系统接收数据,然后必须发布事件或发送命令。问题是,我的组件应该使用哪个事件或命令?

Just to clarify, I'm not talking about the external system knowing anything about my events and/or commands. I have a component that receives data from the external system and then has to publish an event or send a command. The question is, which of event or command should my component use?

推荐答案

这是有限上下文的完美示例。

This is a perfect example of bounded contexts.

在另一个表示相同或业务流程步骤的系统(或有界上下文)中发生的事件不应视为在接收中发生的事件系统。

An event that has occurred in another system (or bounded context) that represent the same or business process step, should not be treated as an event that has occurred in the receiving system.

花店系统借记事件的结构可能与财务跟踪系统中发生的借记事件的结构不同。

The Florist System Debit event might have a different structure than the Debit event that would have occurred in the Finance Tracking System.

要解决此问题,我将拥有一个端点,该端点将监听花店系统事件,并据此使财务跟踪系统根据事件中的信息发出命令,并可能将其与财务跟踪中的信息结合起来系统(如果需要)。在这种情况下,可以将其视为财务跟踪系统的客户端,或者甚至可以作为域服务。

To solve this I would have an endpoint that would listen to the Florist System events and from that I would make the Finance Tracking System issue a command based on the information in the event and maybe combine it with information in the Finance Tracking System if needed. Where this happens could be thought of as a client to the Finance Tracking System or maybe even a "domain service" if you will.

在这种情况下,花店系统附带有在花店系统中发生的交易的信息(有限的上下文)。我不会在域服务中做出任何决定,而是向执行该决定并可能发出财务跟踪系统事件的财务跟踪系统域发出命令。如果花店系统事件在财务跟踪系统中显示为格式错误,则您可能不想以请求/响应或确认/不确认的方式告知花店系统。该事件是从花店发布的,如果您实施类似的操作,则会破坏消息传递模式。您的消息传递基础结构应允许您重试该消息,甚至修复接收代码并使用新代码重试该消息。

In this particular case an event from the Florist system comes in with information of a transaction that has occurred in the Florist system (bounded context). I would not make any decisions in the domain service, but issue a command to the Finance Tracking System domain where the decision takes place and Finance Tracking System events may be emitted. If the Florist system event appears malformed in the Finance Tracking System you probably don't want to tell the Florist system about that in a Request/Response or Ack/Nack manner. The event was published from the Florist and you would break messaging patterns if you implement something like that. Your messaging infrastructure should allow you to retry the message or even fix the receiving code and retry the message with the new code.

但是,如果您确实需要回传消息,在花店有限的上下文中,花店系统可以订阅财务跟踪系统中的事件,以便确定交易是否已成功处理。仅当主系统正在决定是否可以进行交易时才需要这样做。

However, if you really need to communicate back the Florist bounded context, the Florist system could subscribe to events from the Finance Tracking System in order to find out whether the transaction was successfully handled. That would only be needed if the main system is deciding whether a transaction may occur or not.

如果您描述的是财务跟踪系统更多是交易日志,那么您唯一要做的就是不要将花店事件视为财务跟踪系统事件。在导致财务跟踪系统事件的命令之间插入一些内容。

In the case you are describing the Finance Tracking System is more of a transaction log and the only thing you should have to do is to not treat Florist events as Finance Tracking System events. Put something in between that issues commands that result in Finance Tracking System events.

编辑:

作为响应进行编辑。您的接收组件应将命令发送到财务跟踪系统域,该域将依次发出事件(与往常一样)。

As a response to your edit. You receiving component should send commands to the Finance Tracking System domain which in turn would emit events (as usual).

这篇关于来自外部系统的事件源命令或事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆