Definition
CQSR is an architectural pattern which states that while designing the data access layer in a project, it can be split into two separate units of codebase. Those two units of codebase are:
- Command
- Query
So this is called command query separation. In a simpler manner, it is a separation of code which reads data from a database from code which changes the state of data in a database. Thus the responsibility of reading data from a database and writing data to the database can be separated.
Benefits
- Two separate layers can be hosted on the same or multiple hosting environments.
- Most databases need to read data more frequently than to write data to the database, so we can create multiple databases to read from and one single database to write to and then the two databases can be synchronized.
- We can use multiple types of databases e.g. MSSQL for reading and MySQL for writing. So if there is separation of reading and writing then we can write different data access logic for two different types of databases.
- While separating the two things, we must create separate object classes for the Command layer and for the Query Layer. Thus we can create this object model independent of each other. The need of mapping of DTO objects of data layer and Domain model Objects can be avoided.
- Finally working with two separate units of code will improve the performance of the application. Data can be read faster than normal CRUD operations.
- This makes the complex business logic simpler by holding the single responsibility.
- A large team can work separately by dividing the responsibilities among the team members.
These are two ways you can make this happen
- We can separate interfaces for command and Query in the same layer of project.
- We can use two separate layers, one for commands and one for querying databases.


Sachin KhamkareditedPosted Jan 29, 2013, 12:26 AMEdited Jan 29, 2013, 12:27 AM
Good one...can you give an example through coading or small sample code using azure
Atul KumarPosted Jul 27, 2012, 12:58 AM
@Rohatash Kumar: When you create your data layer. you nornally do it using CRUD operations. All the data manipulation and data query code exits in that layer. but using this pattern you just need to break that data access code into two separate projects and then use it in ordinary way.
Atul KumarPosted Jul 27, 2012, 12:56 AM
@Gaurav Gupta: No, it can be done without Wcf services. You can do it by creating separate C# class library project and expose that as a normal .dll library. But we can say that it could be more useful and worth if we do it using WCF services. Because then you can host your business logic on different domain or physical server.
Rohatash KumarPosted Jul 26, 2012, 11:36 PM
can you give a example of two separate layers, one for commands and one for querying databases?
Gaurav GuptaPosted Jul 26, 2012, 11:05 PM
Is it only works with WCF services?
Richa GargPosted Jul 26, 2012, 11:05 PM
Nice explanation........ Can you also give an example through coding?