CQRS and REST HATEOAS mismatch
Asked Answered
F

2

5

Suppose you have a model Foo. One business case is to simply create an instance of Foo, so there is a corresponding CreateFooCommand in my model, triggered by invoking a POST request to a given REST endpoint.

There are of course other Commands too.

But now, there is a ViewModel, which is derived from my DomainModel. It's simply a sql table with raw data - each Foo instance from DomainModel has corresponding derived ViewModel instance. Both have different IDs (on DomainModel there is a DomainID, on ViewModel it's simply a long value).

Now: should I even care about HATEOAS in such a case? In a proper REST implementation, I should at least return location-url in the header. But since my view model is only derived from DomainModel, should I care? I don't even have the view model's ID at the time my DomainModel is created.

Featherweight answered 6/5, 2015 at 10:59 Comment(0)
C
10

Since CQRS means that Queries are separated from Commands, you may not be able to perform a Query right away, because the Command may not yet have been applied (perhaps it never will).

In order to reconcile that with HATEOAS, instead of returning 200 OK from the POST request, the service can return 202 Accepted:

The request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place. There is no facility for re-sending a status code from an asynchronous operation such as this.

The 202 response is intentionally non-committal. Its purpose is to allow a server to accept a request for some other process (perhaps a batch-oriented process that is only run once per day) without requiring that the user agent's connection to the server persist until the process is completed. The entity returned with this response SHOULD include an indication of the request's current status and either a pointer to a status monitor or some estimate of when the user can expect the request to be fulfilled.

(My emphasis)

That pointer could be a link that the client can query to get the status of the Command. When/if the Command completes and the View is updated, that status resource could then contain a link to the view.

This is pretty much a workflow straight out of REST in Practice - very reminiscent of its Restbucks example.

Another option to deal with the ID issue is to generate the ID before accepting the Command - perhaps even asking the client to supply the ID. Read more about such options here.

Curious answered 6/5, 2015 at 13:39 Comment(4)
Thanks for your answer. But with your approach, you're coupling command model with query model. don't you? What if there is more than one corresponding read model?Featherweight
Then add more than one link.Curious
Which kind of breaks the separation between command and query. But thanks anyway, I will surely consider it ;)Featherweight
CQRS isn't a top-level architecture; it's an 'implementation detail'. If you had used CQRS to implement an app targeted at humans (e.g. a web site), would you have had any qualms about putting Commands and Queries into the same UI?Curious
P
1

As Greg Young explains, CQRS is nothing more than "splitting one object into two". So assume that you have one domain aggregate and it has an id. Now you are talking about your view model having another id. However, you are unable to update your view model unless you have the aggregate id in your view model as well. From my point of view, your REST POST request should return a result that has the aggregate id in it. This is your id, the view model id has no interest to anyone except the read model storage.

Should it return a command status URI like Mark suggests is a topic for another discussion. Many CQRS practitioners currently tend to handle commands synchronously to avoid FE/BE mismatch in case of failure and give the FE an ability to react on errors on the BE. There is no real win to execute commands asynchronously for one user. Commands do mutate the state and in 99% of cases the user needs to know if the state was mutated properly.

Pitsaw answered 10/5, 2015 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.