Reputation: 11052
I just learned about CQRS and have been analyzing my existing code, looking for places where I violate or apply CQRS.
So far I haven't found any places where a query is changing state.
However, many commands modify state and return a data object representing the updated state.
For example: An angular client makes an HTTP request to /api/message.post
. The user's id is attached to the request by the HTTP server (based on the HTTP headers) and passed to a microservice that handles message.post
commands. The result of the command handler is {ok: false}
or {ok: true, message: {_id: "xyz", text: "new" ...}}
and the response object is proxied from the microservice back to Angular by the HTTP server.
Is returning state from a command a violation of CQRS?
Should I return only "ok: true/false" from the command, then make a second request to get the new message? If so, why?
Is it worth the overhead if the request/responses are happening through http from a browser?
edit The reason I have the data I need at the end of my command is, my read and write models are currently identical. I do understand that in the future I can apply a write model for commands and a read model for queries, at which point it no longer makes sense to return the data the command wrote.
Upvotes: 6
Views: 2871
Reputation: 16348
A command handler can return a result (ok, error, some data), but if you're using a service bus, a handler can be executed in a background worker and there will be nobody do handle the result.
In your case, I suppose a command handler is executed directly by the server-side controller so you can return a result. However, if the result you're expecting requires a query which has nothing to do with the command, then it's better to have a different query.
If the result contains the command data itself, then you can return it.
Btw, what you're asking is CQS, not CQRS and in a way it's still the single responsibility principle but in a more specialized form.
Upvotes: 4