Command Pattern in GOF vs CQRS meanings
Asked Answered
V

2

8

When Looking at the command pattern a found a slight difference. May be some is able to more clearify this.

When looking at Gang Of Four it says that each command has a Execute Method see for example: http://www.blackwasp.co.uk/Command.aspx like:

myCommand.Execute(myValue);

Now when i look at the commands how there are used in CQRS (Greg Young) i see that this commands don't have an execute method. They are only some kind of "Command Instruction" instance. Similar things are said in the CQRS webcasts like.

And the command is handled by the domainobject. like

class myDomainObject
{
    void UpdateValue(UpdateValueCommand cmd){
        this.value = cmd.value;
    }
}

Is it right that it's just another CommandPattern definition like "CQRS-Command Pattern" ? So that when talking of a command it may have slight diffent meaning in a "common" or "cqrs" context? or am I missing something the command pattern or CQRS implementation?

Voronezh answered 7/5, 2015 at 8:41 Comment(1)
Just in case this helps, I've just published post which was inspired by this question. If you are using CQRS, don't throw the baby out with the bath water. While CQRS commands are different to those described by the Gang of Four, they can still be rather useful. For example, you can create a library of 'GOF Commands' which run at startup or on error. If you're interested you find the post here: Is a CQRS Command = GOF Command?Turret
S
8

Good question.

Command pattern in its raw form is like GOF says.

In CQRS the command is just a DTO (data transfer object) because CQRS is, in most cases, implemented with events or message bus that handles the command. In CQRS you send the command to the system and the system has some kind of bus or event architecture that allows autonomous component to subscribe to handle the command; in this way you can create a responsibility chain and it is more SOLID to, for example, work with read and write models.

It is more a chain of responsibility pattern that command pattern but you keep the advantages of command pattern because you still have commands and can, for example, implement UNDO an action easily like in command pattern.

This link can help you to understand CQRS better.

Soapberry answered 7/5, 2015 at 8:51 Comment(5)
I think you probably mean message-bus, not ESB. The latter doesn't seem very related to cqrsAnagoge
@Anagoge You are right. Just message bus. Thanks for the point.Soapberry
@Soapberry I think the command isn't a DTO but command is just a method that writes. You can use CQRS without any event/message bus. CQRS is just about segregation/isolation of methods that write from methods that read. If you treated command as DTO in a non-RPC based system you would end up with a lot of duplicated code. A command is not a model in the original CQRS and CQS descriptionDocumentary
"CQRS is a pattern that segregates the operations that read data (queries) from the operations that update data (commands)" learn.microsoft.com/en-us/azure/architecture/patterns/cqrsDocumentary
@Konrad. I agree about all the nuances you are putting on the table but remember that the answer is in the context of the related question. I can not give a complete tutorial about CQRS implementations in the answer so I am try to stick to "some kind of Command Instruction" that the OP sees in Greg Young example.Soapberry
C
6

This came up on the DDD/CQRS discussion group, answer by Greg Young:

There are multiple definitions of the command pattern.

You are looking at the Command Message pattern. The only difference is GoF combines the handling of the command with the Command itself. If using the command over a tier boundary this turns out to be a not-so-great idea as your schema and handling are tied together

https://groups.google.com/forum/?hl=en#!topic/dddcqrs/Yfrt4OqPUD0

Cambist answered 7/5, 2015 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.