2

I am beginning a project and I was wondering If I can use CQRS and Hexagonal architecture in the same project, or in fact they are incompatible.

I read about CQRS and some people agree that it is not an architecture, and more like a pattern.

What do you think?

jpadilladev
  • 201
  • 2
  • 6
  • 2
    you were given a [poor advice at Stack Overflow](https://stackoverflow.com/questions/47589215/is-cqrs-incompatible-with-hexagonal-architecture#comment82135826_47589215 "'maybe you would get better responses here softwareengineering.stackexchange.com'") - sorry about that – gnat Dec 01 '17 at 09:40
  • 3
    That advice was not completely wrong. However, it should have been extended by *"... if you add some more details to your question, don't assume anyone knows what Hexagonal architecture is, tell what research you did before and why you think those two approaches are incompatible"*. Currently, it is a very badly written question. – Doc Brown Dec 01 '17 at 21:28
  • I agree that my question is poorly written, sorry about that. Maybe I should add some sources. – jpadilladev Dec 02 '17 at 10:46
  • Yes, please flesh out your question with more details and a link or two. I've never heard of Hexagonal architecture. – user949300 Dec 08 '17 at 17:45

2 Answers2

17

This is like asking if you can use utensils to eat ice cream. (Yes you can. I recommend a spoon.)

CQRS asks you not to mix together the complexity needed to perform queries with the complexity needed to update.

enter image description here

Hexagonal Architecture asks you not to mix the complexity of your input and output ports with your central application logic.

enter image description here

So yes you can do both. But doing one doesn't mean you've done the other.

If you did, it could be diagrammed like this:

enter image description here

That shows where things go. Imagine adapters between the hexes that help isolate the central application logic from DB and UI changes.

Many of these fancy principles are really just about isolating things. It's not that you can't make something that works if you ignore them. But following them means the impact of a requirements change is isolated. It doesn't make it easier to write the code nearly as much as it makes it easier to maintain the code.

Hexagonal, onion, and clean architecture all seem to be different names for the same thing. Authors love to steal an idea and give it a new name.

candied_orange
  • 102,279
  • 24
  • 197
  • 315
  • This is what I was looking for. When reading different articles, some people talked about CQRS as an architecture pattern, so I was a bit confused. I have to read more about these two concepts. – jpadilladev Dec 02 '17 at 10:48
  • It really depends on what you consider to be architecture. No one would be surprised if an architect dictates the floor plan. Some might if the architect dictates the choice of nails. Some might not. – candied_orange Dec 02 '17 at 17:10
1

If I can use CQRS and Hexagonal architecture in the same project

Yes, of course.

I read about CQRS and some people agree that it is not an architecture, and more like a pattern.

Also true.

The result is still fundamentally hexagonal; for instance, if you are implementing a stateless service, command and query requests will still pass through a primary port to the domain model, which fetches the data from the external persistence component using a secondary port. The key idea is that reads will be collecting data from a different secondary port than the one used for writes.

If you weren't using CQRS, then your reads and writes would likely be sharing the secondary port.

VoiceOfUnreason
  • 32,131
  • 2
  • 42
  • 79