cqrs icon indicating copy to clipboard operation
cqrs copied to clipboard

AggregateRoot.commit() implementation breaks transactional semantics

Open goldsam opened this issue 5 years ago • 3 comments

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

The current implementation of AggregateRoot.prototype.commit() invokes this.publish() for each applied event one-by-one iteratively. Unfortunately, this prevents publishing all events as a unit transactionally, which IMHO defeats the purpose of a commit:

https://github.com/nestjs/cqrs/blob/9e4e21a6dbb7b3143ede21c37fd28cf1cd902fb4/src/aggregate-root.ts#L22-L25

Expected behavior

In practice, AggreateRoot.publish() currently forwards to IEventBus.publish() following a call to either EventPublisher.prototype.mergeObjectContext or EventPublisher.prototype.mergeClassContext. Instead of iteratively calling publish(), these APIs should use a publishAll API instead. Surprisingly, publishAll() already exists on the IEventBus interface but does not appear to be used anywhere.

What is the motivation / use case for changing the behavior?

Correct transactional handling of applied aggregate events

goldsam avatar May 31 '20 15:05 goldsam

Also, thank you for nestjs. This is a wonderful project.

goldsam avatar May 31 '20 15:05 goldsam

Thank you @goldsam! Would you like to create a PR to use publishAll() instead?

kamilmysliwiec avatar Jun 01 '20 07:06 kamilmysliwiec

Shouldn't this issue be closed? Or is there something left to do / watch out about this topic?

qortex avatar Jul 14 '22 07:07 qortex