Question

Having Watched this video by Greg Yound on DDD

http://www.infoq.com/interviews/greg-young-ddd

I was wondering how you could implement Command-Query Separation (CQS) with DDD when you have in memory changes?

With CQS you have two repositories, one for commands, one for queries. As well as two object groups, command objects and query objects. Command objects only have methods, and no properties that could expose the shape of the objects, and aren't to be used to display data on the screen. Query objects on the other hand are used to display data to the screen.

In the video the commands always go to the database, and so you can use the query repository to fetch the updated data and redisplay on the screen.

Could you use CQS with something like and edit screen in ASP.NET, where changes are made in memory and the screen needs to be updated several times with the changes before the changes are persisted to the database?

For example

  1. I fetch a query object from the query repository and display it on the screen
  2. I click edit
  3. I refetch a query object from the query object repository and display it on the form in edit mode
  4. I change a value on the form, which autoposts back and fetches the command object and issues the relevant command
  5. WHAT TO DO: I now need to display the updated object as the command made changes to the calculated fields. As the command object has not been saved to the database I can't use the query repository. And with CQS I'm not meant to expose the shape of the command object to display on the screen. How would you get a query object back with the updated changes to display on the screen.

A couple of possible solutions I can think of is to have a session repository, or a way of getting a query object from the command object. Or does CQS not apply to this type of scenario?

It seems to me that in the video changes get persisted straight away to the database, and I haven't found an example of DDD with CQS that addresses the issue of batching changes to a domain object and updating the view of the modified domain object before finally issuing a command to save the domain object.

Was it helpful?

Solution

If you really want to use CQS for this, I would say that both the Query repo and the Write repo both have a reference to the same backing store. Usually this reference is via an external database - but in your case it could be a List<T> or similar.

OTHER TIPS

So what it sounds like you want here is a more granular command.

EG: the user interacts with the web page (let's say doing a check out with a shopping cart).

The multiple pages getting information are building up a command. The command does not get sent until the user actually checks out where all the information is sent up in a single command to the domain let's call it a "CheckOut" command.

Presentation models are quite helpful at abstracting this type of interaction.

Hope this helps.

Greg

Also for the rest of your concerns ...

These are more so concerns with eventual consistency as opposed to CQRS. You do not need to be eventually consistent with CQRS you can make the processing of the command also write to the reporting store (or use the same physical store for both as mentioned) in a consistent fashion. I actually recommend people to do this as their base architecture and to later come throught and introduce eventual consistency where needed as there are costs azssociated with it.

In memory, you would usually use the Observer design pattern.

Actually, you always want to use this pattern but most databases don't offer an efficient way to call a method in your app when something in the DB changes.

The Unit of Work design pattern from Patterns of Enterprise Application Architecture matches CQS very well - it is basically a big Command that persist stuff in the database.

JdonFramework is CQRS DDD java framework, it supply a domain events + Asynchronous pattern, more details https://jdon.dev.java.net/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top