Question about output flow #179
Closed
AndrejMitrovic
started this conversation in
General
Replies: 1 comment
-
Hi @AndrejMitrovic, I think you've described the usual pattern correctly here. The "tricky" part arises because we return events from the command handler rather than applying them immediately (i.e., when you would add them to a mutable That being said we've also repeatedly considered moving to the apply-immediately pattern for just this reason. As a side note, it's best to use CQRS in situations where the business logic is quite complex. For your simple problem there may be better ways to do this. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
First of, appreciate the work on this nice library! ❤️
I've been exploring using cqrs for a simple use-case.
The use-case is:
So I considered the following approach:
UploadFileA
,UploadFileB
.FileAUploaded
,FileBUploaded
.file_a: Option<String>; file_b: Option<String>;
.And now comes the tricky part. I need to which part of the cqrs system needs to be responsible for publishing a message to SNS once both files were uploaded. Currently I'm doing it with a Query.
I considered having a 3rd command
PublishFilesToSNS
with an equivalent event. But I don't know which part of the system would be pushing this command.Aggregate.apply()
is not the right place. Only themain()
routine has access tocqrs.execute
, so I don't see a way for any other part of the system to add new commands.An alternative is to not have a 3rd command, but rather only have a 3rd event
BothFilesUploaded
. And thenAggregate.handle()
would manually fire this event if bothUploadFileA
andUploadFileB commands were received for a particular aggregate. But this is tricky, because we're no longer comparing just the state of the aggregate, but the state of the aggregate plus the command in flight
UploadFileA,
UploadFileB`.But this latter approach is the one I took. Visually:
UploadFileA
command fired.Aggregate.handle()
=> PrepareFileAUploaded
event.FileAUploaded
event emitted.Aggregator.apply()
=> Setself.file_a = Some(event.file_name)
.And then:
UploadFileB
command fired.Aggregate.handle()
=> PrepareFileBUploaded
event.Aggregate.handle()
=> Realize both files are ready (tricky). Return[FileBUploaded, BothFilesUploaded
events.Aggregator.apply()
=> ProcessesFileBUploaded
event. Setsself.file_b = Some(event.file_name)
.Aggregator.apply()
=> IgnoresBothFilesUploaded
event.Query.dispatch()
=> ProcessesBothFilesUploaded
event. Pushes something to SNS.This is the approach I have. But this looks very dirty to me as
Aggregator.handle()
needs to do tricky checks with the state of the aggregate and the command that's being processed, plus construct some elaborate event out of it.Maybe this is better done with Views?
Would love to hear your input. Thank you!
Beta Was this translation helpful? Give feedback.
All reactions