事件聚合器错误处理回滚 [英] Event Aggregator Error Handling With Rollback

查看:165
本文介绍了事件聚合器错误处理回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究开发人员在域驱动设计上设计/构建应用程序的许多常见方法(仍然试图理解整体的概念)。我看到的一些例子包括通过事件聚合器使用事件。我喜欢这个概念,因为它真正地保持了应用程序的不同元素/域脱钩。

I've been studying a lot of the common ways that developers design/architect an application on domain driven design (Still trying to understand the concept as a whole). Some of the examples that I saw included the use of events via an event aggregator. I liked the concept because it truly keeps the different elements/domains of an application decoupled.

我所关心的是:如何回滚一个操作一个错误?

A concern that I have is: how do you rollback an operation in the case of an error?

例如:

说我有一个订单应用程序必须将订单保存到数据库,并将订单的副本作为pdf保存到CMS。该应用程序触发已创建新订单的事件,订阅此事件的pdf服务会保存PDF。同时当向数据库提交订单更改时,抛出异常。问题是pdf已被保存,但它们不是匹配的数据库记录。

Say I have an order application that has to save an order to the database and also save a copy of the order as a pdf to a CMS. The application fires an event that a new order has been created and the pdf service that subscribes to this event saves the pdf. Meanwhile when committing the order changes to the database an exception is thrown. The problem is that the pdf has been saved but their isn't a matching database record.

我应该缓存以前处理的事件,并触发一个新的错误事件,看起来是缓存的撤消操作?使用像这样的命令模式吗?

Should I cache the previously handled events and fire a new error event that looks to the cache for "undo" operations? Use something like the command pattern for this?

或...是事件聚合器不是一个很好的模式。

Or... is the event aggregator not a good pattern for this.

编辑

我开始认为应该使用事件更少的关键任务项目,如电子邮件和日志记录。

I'm starting to think that maybe events should be used for less "mission critical" items, such as emailing and logging.

我最初的想法是通过使用事件聚合器模式来限制依赖。

My initial thought was to limit dependencies by using the event aggregator pattern.

推荐答案

您希望在与数据库上的操作相同的事务中提交事件。

You want the event to be committed in the same transaction as the operation on your database.

在这种特殊情况下,可以将事件推送到队列,其中涉及您的交易,以便事件永远不会出来,除非聚合持续。这将使创建PDF最终一致;如果创建PDF失败,您可以解决问题,并自动重试。

In this particular scenario, you can push the event on a queue, which enlists in your transaction, so that the event will never go out unless the aggregate is persisted. This will make creating the PDF eventual consistent; if creating the PDF fails, you can fix the problem, and have it automatically retried.

也许您可以在以前的一个帖子中获得更多的灵感,其中与RavenDB和IronMQ最终一致的域事件

Maybe you can get more inspiration in one of my previous posts on eventual consistent domain events with RavenDB and IronMQ.

这篇关于事件聚合器错误处理回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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