Friday 19 February 2010

CQRS the simple way.

CQRS stands for Command Query Responsibility Segregation and is a pattern that splits an application into two parts: Commands and Queries.
Queries are used to get data from a datasource (like a database) and commands are used to do something with the data.
The simplest form of CQRS is the following:


public interface BlogQueryService
{
    IEnumerable<Blog> GetAllBlogPosts();
    Blog GetBlogPost(int blogid);
    IEnumerable<Comment> GetAllCommentsForPost(int blogid);
}

public interface BlogCommandService
{
    void NewBlogPost(string title, string text);
    void EditBlogPost(Blog editedBlog);
    void AddComment(int blogid, string comment);
}

The BlogCommandService just updates the datasource with the given data (and does validation if needed) the query service just return simple data.
Of course this is not scalable and a lot of advantages that Udi Dahan and Greg Young describe are not here, but it is a first step to decouple a lot of applications.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.