Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement CQS with in memory changes?

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.

like image 786
Ian Avatar asked Sep 25 '09 11:09

Ian


People also ask

What is the difference between CQS and CQRS?

CQRS takes the defining principle of CQS and extends it to specific objects within a system, one retrieving data and one modifying data. CQRS is the broader architectural pattern, and CQS is the general principle of behaviour.

Should I use Repository pattern with CQRS?

If you're applying CQRS and Vertical Slice Architecture you'll likely want a repository to build up Aggregates. However, for a Query, you may want to just get the data you need rather than an entire aggregate (or collection of aggregates) to build a view model.

What problem does CQRS solve?

CQRS is a popular architecture pattern because it addresses a common problem to most enterprise applications. Separating write behavior from read behavior, which the essence of the CQRS architectural pattern, provides stability and scalability to enterprise applications while also improving overall performance.


2 Answers

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

like image 116
Greg Young Avatar answered Nov 15 '22 10:11

Greg Young


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.

like image 43
Erik Forbes Avatar answered Nov 15 '22 10:11

Erik Forbes