I am trying to implement the CQRS pattern.
Following the CQRS pattern, the Command
should return nothing, as the void
type in Java.
The create()
method in the repository is the command in my opinion and it should return nothing following the pattern.
But I am struggling to build the function which needs to create and use the new entity.
For example, I have to build a function assign new child category
. In this feature, the client can either send the parent category id
or new child details
to the server.
The parent category id
is an easy case, but the problem is in the new child details
case.
I have to create a new category base on the details first, then use that entity to assign the parent id to it, and finally save it. Currently, I am not able to get the newly created entity, because the create()
method returns nothing.
And I can't create a new entity, assign the parent id and save it, because the entity can't be created without an id and the id is only assigned by the ORM I am using.
What should I do to solve this problem? Does the method create()
of the repository return an entity other than void
violate the CQRS pattern?
It depends on scenario & business needs. All the design patterns, approaches, etc. just give you an idea of possible solutions to common problems. There is no single good principle to follow that works for all the scenarios across different business problems. If after executing the command, each time you need to execute some logic based on the result - then return that result to avoid unnecessary calls. It all depends on the problem you try to solve & trade-offs you are ready to make.
For more info you can check this question.