15

I am attempting to learn and apply the CQRS design approach (pattern and architecture) to a new project but seem to be missing a key piece.

My client application executes a query and retrieves a list of light-weight, read-only DTOs from the read model. The user selects an item and clicks a button to initiate some action. The action is performed by creating and sending the corresponding command object to the write model (where the command handler carries out the action, updates the data store, etc.) At some point, however, I need to update the UI to reflect changes to the state of the application resulting from the action.

How does the UI know when it is time to refresh the original list?

Additional Info

I have noticed that most articles/blogs discussing CQRS use MVC client apps in their examples. I am working on a Silverlight client right now and am beginning to wonder if the pattern simply doesn't work in that case.

Follow-Up Question

After thinking more about Bartlomiej's response and subsequent discussion, I am wondering about error handling in CQRS. Given that commands are basically fire-and-forget asynchronous operations, how do we report an error condition to the UI?

I see 'refreshing the UI' to take one of two forms:

  1. The operation succeeds, data has changed and the UI should be updated to reflect these changes
  2. The operation fails, data has not changed but the user should be notified of the failure and potential corrective actions.

Even with a Post-Redirect-Get pattern in an MVC, you can't really Redirect until you know the outcome of the operation. None of the examples I've seen thus far address these real-world concerns.

4 Answers 4

4

I've been struggling with similar issues for a WPF client. The re-query trigger for any data is dependent on the data your updating, commands tend to fall into categories:

  1. The command is a true fire and forget method, it informs the back-end of a state change but this change does not need to be reflected in the UI, or the change simply isn't important to the UI.

  2. The command will alter the result of a single query

  3. The command will alter the result of multiple queries, usually (in my domain at least) in a cascading fashion, that is, changing the state of a single "high level" piece of data will likely affect many "low level" caches.

My first trigger is the page load, very few items are exempt from this as most pages must assume data has been updated since it was last visited. Though some systems may be able to escape with only updating financial and other critical data in this way.

For short commands I also update data when 'success' is returned from a command. Though this is mostly laziness as IMHO all CQRS commands should be fired asynchronously. It's still an option I couldn't live without but one you may have to if your implementation expects high latency between command and query.

One pattern I'm starting to make use of is the mediator (most MVVM frameworks come with one). When I fire a command, I also fire a message to the mediator specifying which command was launched. Each Cache (A view model property Retriever<T>) listens for commands which affect it and then updates appropriately. I try to minimise the number of messages while still minimising the number of caches that update unnecessary from a single message so I'll (hopefully) eventually end up with a shortlist of update reasons, with each 'reason' updating a list of caches.

Another approach is simple honesty, I find that by exposing graphically how the system updates itself makes users more willing to be patient with it. On firing a command show some UI indicating you're waiting for the successful response, on error you could offer to retry / show the error, on success you start the update of the relevant fields. Baring in mind that this command could have been fired from another terminal (of which you have no knowledge) so data will need to timeout eventually to avoid missing state changes invoked by other machines also.

Noting the irony that the only efficient method of updating cache's and values on a client is to un-separate the commands and queries again, be it through hardcoding or something like a hashmap.

3
  • Nice summary and sounds like you're close to the same line of thought I've come to since my original post. I opted not to use Domain Events to communicate pass/fail and am leveraging the Task Parallel Libraries (TPL) to address my needs while maintaining asynchronicity. In my case, when I send a message to the command bus, I get a Task in return. This allows me to receive notification when the async operation has completed and support passing exceptions back to the calling code. Now I can simply refresh the data in the task's continuation. Aug 28, 2012 at 18:31
  • I believe this is consistent with CQRS because the command and query are still separated but gives me better control from the UI where, as you said, you have to un-separate them if the UI is to function as desired/expected. Aug 28, 2012 at 18:32
  • Plus, this fits into MVVM very nicely! Aug 28, 2012 at 18:32
2

My two cents.

I think MVVM actually fits into CQRS quite well. The ViewModel simply becomes an observable ReadModel.

1 - You initialize your ViewModel state via a query on the ReadModel.

2 - Changes on your ViewModel are automatically reflected on any Views that are bound to it.

3 - Certain changes on your ViewModel trigger a command to propegate to a message queue, an object responsible for sending those commands to the server takes those messages off the queue and sends them to the WriteModel.

4 - Clients should be well formed, meaning the ViewModel should have performed appropriate validation before it ever triggered the command. Once the command has been triggered, any event notifications can be published onto an event bus for the client to communicate changes to other ViewModels or components in the system interested in those changes. These events should carry the relevant information necessary. Typically, this means that other view models usually don't have to re-query the read model as a result of the change unless they are dependent on other data that needs to be retrieved.

5 - There is an object that connects to the message bus on the server for real-time push notifications when other clients make changes that this client is interested in knowing about, falling back to long-polling if necessary. It propagates those to the internal message bus that ties the components on the client together.

6 - The last part to handle is the fact that clients can be occasionally connected, which should be the only reason a command fails (they don't have internet access at the moment), which is when the client should be notified of problems.

3
  • Also, handle the occasionally connected client in terms of events that should have been propagated to it from the server while it lost connection (if it has only been disconnected for a while). There would probably be a certain threshold of time that would require the client to re-initialize entirely from the ReadModel. Nov 13, 2013 at 2:16
  • I am struggling with this issue as well. I totally agree that the browser send a command to server(async or sync? I am not sure which is the right way, but leave it for now), server consume the command and make some state changes, and then publish a changed event with some information indicating the changes. My question is that how can we design this changed event which could be readable by both browser javascript which will modify the ui and some server side stuffs which are interested in this event?
    – Ron
    Apr 10, 2014 at 14:49
  • This changed event should be sent to the server using JSON. It should carry only the data necessary to perform any action the server would require. I.e. the new state of the object, the original state of the object should already be present on the server. The server should be able to match up the object that is changing state by using some sort of UUID. Jul 24, 2014 at 16:25
1

In my ASP.NET MVC 3 I use 2 techniques depending on use case:

  • already well-known Post-Redirect-Get pattern which fits nicely with CQRS. Your MVC action that triggers the command returns a redirection to action that performs a query.
  • in some cases, like real-time updates of other clients, I rely on domain events/messages. I create an event handler that uses singlarR to push changes to all connected and interested clients.
5
  • That may explain why every CQRS example I've seen uses an MVC client. Unfortunately, I'm working with Silverlight (possibly switching to WPF in the near future). And that approach won't work there. But, good information to have for the next time I work with ASP.NET MVC. Jul 5, 2012 at 12:47
  • Am I trying to force a square peg (CQRS) into a round hole (Silverlight)? Jul 5, 2012 at 12:48
  • Do you use MVVM pattern in your Silverlight app? Jul 5, 2012 at 16:02
  • Here is a sample SL app. Maybe you'll find the answer by reviewing that code: thecqrskitchen.codeplex.com Jul 5, 2012 at 16:27
  • I am using MVVM. It appears that the example has the ViewModel handling an event then re-issuing the query to refresh the data. However, it appears that the event is raised before the command actually executes. This makes a pretty big assumption about the timing as the action could take a bit of time to complete on the server and refreshing the data before the call has even started seems like a sure-fire way of retrieving unchanged state. Since the action ultimately makes a call to a web service, wouldn't it be better to raise the event when the service call returns? Jul 5, 2012 at 20:25
0

There are two major ways you can take as far as I know :

1) design your UI , so that the user does not see its changes right away. Like for instance a message to tell him his action is a success, and offering him different choices to continue his work. this should buy you enough time to have updated your readmodel.

2) more complex, but you might keep the information you have send to the server and shows them in the interface.

The most important I guess, educate your user if you can so that they know why the data is not here... yet!

I am thinking about it only now, but these are for sync command handling, not async, in async things go really harder on the brain...the client interface becomes an event eater too..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.