-3

Command Query Responsibility Segregation and Model–view–controller patterns look pretty similar to me.

Are they comparable? Do they act at the same layer of abstraction? How do they differ? Can they be used together or one replaces the other? What am I missing?

4
  • 2
    gona have to give me a bit more to go on. why do you think they are similar?
    – Ewan
    Jan 5, 2021 at 10:07
  • 1
    MVC is about separating model, M, from user interface, V&C, for clarity & maintainability. CQRS doesn't have any user interface; it separates model query from model change, allowing different representation for query vs. update, for performance reasons.
    – Erik Eidt
    Jan 5, 2021 at 11:44
  • 1
    Note that MVC is specifically addressed to solve a front-end issue. It's not an architectural style. Both things solve different problems. Like many other solutions, both lay on well-known practices like segregation of concerns, SRP, etc but here ends all the similarities.
    – Laiv
    Jan 5, 2021 at 11:57
  • 1
    Religious beliefs notwithstanding, MVC is commonly regarded as an architectural pattern. But more to the point, no less than Greg Young has suggested that CQRS can be seen as an availability layer mediating MVC-read and MVC-write. Cf. codebetter.com/gregyoung/2010/09/07/cqrs-and-mvc
    – Forest
    Jan 31 at 18:52

1 Answer 1

1

MVC and CQRS are apples and hand grenades. MVC is about routing user actions from a view and returning data. CQRS is a data access pattern to avoid side effects in queries and provide a simple scaling solution. Part of the problem is that model is probably one of the most used words in patterns and not all of them are mutually exclusive.

MVC and CQRS can be used together or separately. The model from MVC is really a UI level representation of data structures, the physical data model need not be anything similar. It is generally best practice for MVC Models to call some other thing (like a service) to get/save data, in a CQRS world the model would be calling separate things for read/write actions. Where data is persisted is somewhat outside the concern of MVC, so long as the model knows how to access data and nothing else does.

CQRS is concerned with isolating all reads (Query) to only read and never write, and all writes/creates/updates (command) to only write. The model used to write should also be different than the model returned from a read, which in most normalized relational databases is almost self fulfilling. Furthermore the database or whatever is used for persistence doesn't need to be the same one for commands and queries. If the same web app is doing both reads and writes through the respective services, that is outside the scope of CQRS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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