I know there are many posts on this question but none of them gives a clear answer or solution (atleast for my specific case)
Some of them are
In CQRS/ES, can a command create another command?
https://medium.com/workleap/why-command-should-not-call-command-in-cqrs-5da046a9fed1
However the general advice in all the posts is that we should not call one command inside another command.
Let me explain my specific case here now
- I have two type of entities
Document
andForm
. - A document can have multiple forms
- There is a
CreateDocument
command. Document can be created without any child forms - There is a
CreateForm
command. Form can be created for any existingDocument
- So both
Document
andForm
can be created separately. - While creating the
Document
ifForm
is received then the forms should also be created and that is where the issue comes
Now I have separate commands for both document and form creation but forms can also be created as a part of document creation.
So the problem comes when I receive forms as part of document creation.
In this case both document and form should be created but there commands are separately written.
Possbile solutions
- Should I duplicate the whole code of form creation inside
CreateDocument
command? - Should I call the
CreateForm
command insideCreateDocument
command (This is nowhere recomended)? - Should I trigger an
event
after the document is created and that event handler will handle the creation of forms if any?
Problems with above solutions
Solution 1: This is the cleanest and most correct in the sense that it will allow me to do all the operations under one transaction
which is required for my case. But the downside is that I will have to duplicate a lot of code, I am not sure if it is the right practice.
Solution 2 - This is the easiest but it is not recomended anywhere. Also, it lacks the posibility of doing everything in one transaction, so it is possible that document is saved but forms saving fails.
Solution 3 - This is similar to solution 2 but is better approach as I have read but this again lacks the transaction.
Also, the events are captured using INotificationHandler<T>
types, so this is also not a command, so I am not sure if I should again call the command inside the event handler to create forms?
Your suggestions please