jeudi 3 mars 2016

Cutting Edge - Command and Query Responsibility Segregation for the Common Application

When we decide to start any project, we have to think about the architecture, and before using any pattern, we attempt to improve upon the stereotypical architecture in small rational steps while trying to minimize the cost in terms of productivity for each step towards a better architecture. 
Domain-Driven Design (DDD) is used about a decade ago, inspiring software developers and architects, we can get more details about this approach from this link :  written by Eric Evans and this book  where you can get answers in ways of making the design of your software independently from your problem and your project, it let you realize your dream of building applications around a comprehensive object model to address all stakeholder requirements and concerns, because it's old, we find an important strategic concept of DDD called Bounded Context. (
Recently, another method with new acronym started gaining scale,it is not a pattern like DDD but it's a concept  built on DDD called Command and Query Responsibility Segregation (CQRS). The fundamental aspect of a CQRS solution is the separation between the command stack and query stack.
In CQRS, The command stack is concerned only about the performance of tasks that modify the state of the application, each stack is designed separately; such a distinct design potentially can lead to having two distinct domain layers and even two distinct databases. As usual, the application layer receives requests from the presentation and orchestrates their execution. 
But, can we  consider CQRS a rework of the classic multi-layered architecture that leaves the door open for more changes and evolution ?
If we start using CQRS, you will not find the need to have a distinct section of the domain layer for just reading data. Plain queries out of ready-made tables are all you need. How ? The query stack is concerned with data retrieval, it uses a data model that matches the data used in the presentation layer as closely as possible, so, it performs read-only operations against te back end that don't alter the state of the system. In the implementation of a modern query stack, the LINQ language in Microsoft .NET framework is helpful. So, we suppose that you have a  ASP.NET MVC Web application that select a list of clients data to display in different forms, our system is organized in layers. and the application services invoked directly from controllers that orchestrate use cases. We consider the application layer as platform from which you run commands and queries against the rest of the system, so if you try to apply CQRS, so ,we will use two distinct middle tiers, first one takes care of commands that alter the system state and the other retrieves the data.
In this case, you have  created a couple of class library projects—query stack and command stack—and reference both from the main Web server project. 

1 commentaire: