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!