www.arnaudlanglade.com
Open in
urlscan Pro
2606:50c0:8000::153
Public Scan
URL:
https://www.arnaudlanglade.com/difference-between-cqs-and-cqrs-patterns/
Submission: On September 12 via manual from GB — Scanned from GB
Submission: On September 12 via manual from GB — Scanned from GB
Form analysis
2 forms found in the DOMPOST https://github.us13.list-manage.com/subscribe/post?u=d7313775f348f8c8d39a841a0&id=28bcb90eaa&f_id=00550ee4f0
<form action="https://github.us13.list-manage.com/subscribe/post?u=d7313775f348f8c8d39a841a0&id=28bcb90eaa&f_id=00550ee4f0" method="post" class="text-center">
<h3 class="title fw-bold">Newsletter: Be the first to know!</h3>
<p>Subscribe to my newsletter for updates on my latest blog posts, tech releases, and exclusive content. Stay ahead in the coding game!</p><input type="email" class="form-control" placeholder="Your email address" name="EMAIL">
<button type="submit" class="btn btn-primary my-4 fw-bold fs-4 text-light px-5 py-3 text-dark"> Subscribe</button>
<p class="privacy">Your data will remain strictly confidential and will be used exclusively to communicate with you. Be assured that it will never be sold to third parties.</p>
</form>
POST https://github.us13.list-manage.com/subscribe/post?u=d7313775f348f8c8d39a841a0&id=28bcb90eaa&f_id=00550ee4f0
<form action="https://github.us13.list-manage.com/subscribe/post?u=d7313775f348f8c8d39a841a0&id=28bcb90eaa&f_id=00550ee4f0" method="post">
<h3 class="fs-5 fw-semibold text-primary mb-2">Subscribe to my newsletter</h3>
<div class="input-group"><input type="email" class="form-control" placeholder="Your email address" name="EMAIL">
<button type="submit" class="btn btn-primary text-dark">Subscribe</button>
</div>
<p class="privacy mt-4 text-light">Your data will remain strictly confidential and will be used exclusively to communicate with you. Be assured that it will never be sold to third parties.</p>
</form>
Text Content
Arnaud Langlade * Home * Training * Blog * Talks * MikadoApp * Hire me * Software-Architecture Design-Patterns WHAT IS THE DIFFERENCE BETWEEN CQS AND CQRS PATTERNS? Published at Feb 6, 2023 Image by @mihaistrompl I recently found out that I did not grasp those design patterns. There are a lot of resources on the Internet about them but they are not always accurate. That’s a shame because they are pretty simple. I will share my understanding of them with you. WHAT IS COMMAND QUERY SEGREGATION (CQS)? > The fundamental idea is that we should divide an object’s methods into two > sharply separated categories: > > * Queries: Return a result and do not change the observable state of the > system (are free of side effects). > * Commands: Change the state of a system but do not return a value. > > Martin Fowler This concept is not specific to Object Oriented Programming but improves the object’s design. The object methods only have a single purpose: reading or changing the object state. We can see an object as a living entity. We can ask a question to someone because we need information. For example, we can ask someone what time it is. This is a query. We can ask someone to do something, we don’t expect an answer but we want to get the job done. For example, we can ask a child to finish his/her spinach. This is a command. We can apply this pattern to any object: like an aggregate. Let’s take an example! I would like to add markers on a map and then I would like to find which markers are the closest to a specific location (GPS Coordinates). k class Map { addMarker(label: string, latitude: number, longitude: number): void { // ... } findClosestMarkers(location: Location): Marker[] { // ... } } The addMarker method is in charge of mutating the object state without returning any result, while the ‘findClosestMarkers’ method finds the right markers without changing the object’s state. This object follows the CQS definition. Do you speak French ? Tired of the same old CRUD applications, struggling with your framework, or feeling the pressure of production releases? It’s time to take your career to the next level. Discover the power of Hexagonal Architecture and DDD to build robust and sustainable Symfony applications. Join me and kickstart your journey toward mastering advanced development techniques. 🚀 Start your journey here! Let’s go further. If we design our aggregates following the CQS pattern, we should apply it to the classes that handle use cases. interface MapService { addMarkerToTheMap(label: string, latitude: number, longitude: number); void findAllMarkersCloseToLocation(): Marker[] } This ensures there is no inconsistency in the codebase. The business services use and manipulate the aggregates. For example, if the MapService.addMarkerToTheMap method returns a result, it might mean that the Map.addMarker method will need to return the expected result. WHAT IS COMMAND QUERY RESPONSIBILITY SEGREGATION (CQRS)? > Starting with CQRS, CQRS is simply the creation of two objects where there was > previously only one. The separation occurs based upon whether the methods are > a command or a query (the same definition that is used by Meyer in Command and > Query Separation, a command is any method that mutates state and a query is > any method that returns a value). > > Greg Young Note: Greg Young’s blog does not exist anymore but his blog posts are still available thanks to archived.org. CQRS is the separation of command and query into two different objects instead of only one. MapService does not follow the CQRS pattern because it has a query and a command. We need to cut this object in half. interface MapReadService { addMarkerToTheMap(label: string, latitude: number, longitude: number); void } interface MapWriteService { findAllMarkersCloseToLocation(): Marker[] } That’s pretty simple, right? Anyway, we don’t need to introduce complicated things in our application to use this tactical design pattern. We don’t need a write and a read model, a command and query bus, an event sourcing architecture or multiple databases. Greg Young published this blog post in 2012 to explain what CQRS was not about. > CQRS is not a silver bullet > > CQRS is not a top level architecture > > CQRS is not new > > CQRS is not shiny > > CQRS will not make your jump shot any better > > CQRS is not intrinsically linked to DDD > > CQRS is not Event Sourcing > > CQRS does not require a message bus > > CQRS is not a guiding principle / CQS is > > CQRS is not a good wife > > CQRS is learnable in 5 minutes > > CQRS is a small tactical pattern > > CQRS can open many doors. Note: This blog does not exist anymore but it has been archived by archived.org. The post is available here Depending on the number of use cases the service classes can become really huge. CQRS helps to decrease their size but I am a big fan of them. I like to separate each use case into a dedicated class. I’ve written a blog post to explain what is a commands and we can apply it to query too: Command and command handler design pattern Thanks to my proofreader @LaureBrosseau . HELLO, I’AM ARNAUD! I’m a technical coach and software architect. I aim to make complex software concepts accessible and interesting for all. Learn more about me Join me on social media where I share insights on software development and much more. * * * * * * * Share the post it if you like it: * * * RELATED POSTS THE REPOSITORY DESIGN PATTERN design-patterns OOP repository DDD Discover the repository design pattern explained thanks to a simple example. Learn how it works and it ensures a clean separation between the domain model and the persistence model, aiming to hide the complexity of the object’s state storing. Apr 8, 2024 HOW TO REDUCE COUPLING IN YOUR REACT APP react SOLID design-patterns The dependency inversion principle is a great design pattern, it makes applications more modular and easier to test. A React Context can help to implement this pattern in a React application. Learn how in this new blog article. Mar 6, 2023 HEXAGONAL ARCHITECTURE BY EXAMPLE software-architecture The hexagonal architecture, or ports and adapters architecture, is an architectural pattern used in software design. It aims at creating loosely coupled application components that can be easily connected to their software environment by means of ports and adapters. Jan 9, 2023 NEWSLETTER: BE THE FIRST TO KNOW! Subscribe to my newsletter for updates on my latest blog posts, tech releases, and exclusive content. Stay ahead in the coding game! Subscribe Your data will remain strictly confidential and will be used exclusively to communicate with you. Be assured that it will never be sold to third parties. ARNAUD LANGLADE Join me on social media where I share insights on software development and much more. * * * * * * * LEARN MORE * Home * About me * Training * My talks * Testimonials * Blog * Hire me * MikadoApp * Newsletter SUBSCRIBE TO MY NEWSLETTER Subscribe Your data will remain strictly confidential and will be used exclusively to communicate with you. Be assured that it will never be sold to third parties. © 2024 Arnaud Langlade