使用 JOliver EventStore 更新多个聚合
我对使用 JOliver 的事件存储在单个事务中更新多个聚合有疑问。据我了解,每个聚合都应该有自己的事件流。现在,虽然许多命令处理程序将仅加载单个聚合并仅更新该聚合(即保存这些聚合的事件),但我可以想象将会有命令处理程序需要更新多个聚合。当然,我想以交易的方式做到这一点。
但是,我不知道如何使用事件存储来做到这一点。存储事件是通过在事件流上调用 CommitChanges()
来完成的。如果我们要更新多个聚合,则将有多个事件流,从而多次调用 CommitChanges()
。实现事务性的唯一方法是将其包装在 TransactionScope
中,但这没有多大意义,因为底层存储技术可能不支持事务。所以我最终得到了这段代码,这绝对不是我想要的:
Guid aggregateGuid1 = Guid.NewGuid();
Guid aggregateGuid2 = Guid.NewGuid();
Guid commitGuid = Guid.NewGuid();
var stream = store.OpenStream(aggregateGuid1, 0, int.MaxValue);
stream.Add(new EventMessage() { Body = new MonitorDisabled { MonitorGuid = aggregateGuid1, User = "A" } });
stream.CommitChanges(commitGuid);
stream = store.OpenStream(aggregateGuid2, 0, int.MaxValue);
stream.Add(new EventMessage() { Body = new MonitorEnabled { MonitorGuid = aggregateGuid2, User = "B" } });
// Can't commit twice with the same commit id, what if fails after first one? No way for the store to know it had to write the second part of the commit.
stream.CommitChanges(commitGuid);
这让我觉得我完全错过了应该如何使用事件存储的一些东西。有人可以帮我吗?多谢!
I'm having a question regarding updates to multiple aggregates in a single transaction using JOliver's Event Store. As I understand, every aggregate should have its own event stream. Now, while many command handlers will only load a single aggregate and only update that aggregate (i.e. save events for those aggregates), I can imagine that there will be command handlers which need to update multiple aggregates. And of course, I would like to do that in a transactional way.
However, I don't see how I could do that with the Event Store. Storing events is done by calling CommitChanges()
on an event stream. If we're having multiple aggregates to update, will have multiple event streams and thus multiple calls to CommitChanges()
. The only way to make that transactional is to wrap it in a TransactionScope
, but that does not make much sense, since the underlying storage technology might not support transactions. So I end up with this code, which is definitely not what I am looking for:
Guid aggregateGuid1 = Guid.NewGuid();
Guid aggregateGuid2 = Guid.NewGuid();
Guid commitGuid = Guid.NewGuid();
var stream = store.OpenStream(aggregateGuid1, 0, int.MaxValue);
stream.Add(new EventMessage() { Body = new MonitorDisabled { MonitorGuid = aggregateGuid1, User = "A" } });
stream.CommitChanges(commitGuid);
stream = store.OpenStream(aggregateGuid2, 0, int.MaxValue);
stream.Add(new EventMessage() { Body = new MonitorEnabled { MonitorGuid = aggregateGuid2, User = "B" } });
// Can't commit twice with the same commit id, what if fails after first one? No way for the store to know it had to write the second part of the commit.
stream.CommitChanges(commitGuid);
This makes me feel I'm completely missing something on how the Event Store should be used. Could anybody help me out here? Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
聚合定义事务边界。
如果您需要执行跨聚合事务,您应该检查您的聚合,并可能重新设计它们。
如果一项操作(命令)影响多个聚合,并且您确信聚合设计良好并映射到域中的真实一致性边界,最终一致性可能就是您正在寻找的。只需向每个聚合发送一个命令,并有两个事务,每个事务一个。如果您觉得最终一致性不适合您的情况,那么恐怕它又回到了绘图板。
An Aggregate defines a transaction boundary.
If you need to perform cross-aggregate transactions you should review your aggregates and maybe redesign them.
In cases where an operation ( command ) affects more than one aggregate, and you are sure that your aggregates are well design and map to real consistency boundaries in your domain, eventual consitency might be what you are looking for. Just send a command to each aggregate, and have two transactions , one for each of them. If you don't feel eventual consistency is right for your case than i'm afraid it's back to the drawing board.
我不能代表约翰·奥利弗,但我认为答案是“不要”。聚合是事务边界。如果您需要协调多个聚合的提交,则需要一个显式的协调过程(例如传奇),它将执行并在必要时撤消相关事件。
I can't speak for John Oliver, but I think the answer is "don't". Aggregates are transaction boundaries. If you need to coordinate the commit of several aggregates, you need an explicit coordination process, like a saga, that will do and if necessary undo the relevant events.
有时,如果您的基础设施支持分布式事务,那么在这些场景中可以更轻松地利用分布式事务。
TransactionScope 与 EventStore:
使用 RavenDB 进行跨文档事务:
Sometimes it can be easier to take advantage of distributed transactions in these scenarios if your infrastructure supports them.
TransactionScope with EventStore:
Cross document transactions with RavenDB: