Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Command Query Responsibility Segregation (CQRS) with ASP.NET MVC?

I have been reading about Command Query Responsibility Segregation (CQRS). I sort of wonder how would this work with ASP.NET MVC? I get the idea of CQRS conceptually it sounds nice and sure does introduce some complexities (event and messaging pattern) compared to the "normal/common" approach . Also the idea of CQRS sort of against the use of ORM in some ways. I am trying to think how I could use this pattern in the coming projects so if anyone has experience in combining CQRS with ASP.NET MVC and NHibernate please give some concrete examples to help me better understand CQRS and use with ASP.NET MVC. Thanks!

Updated: I've been going through Mark's sample code. It's a must read if you are learning CQRS.

http://github.com/MarkNijhof/Fohjin

http://cre8ivethought.com/blog/2009/11/12/cqrs--la-greg-young/

http://cre8ivethought.com/blog/2009/11/28/cqrs-trying-to-make-it-re-usable/

like image 237
Jeff Avatar asked Mar 08 '10 11:03

Jeff


3 Answers

Please take a look at my DDDsample.Net project on CodePlex. The GUI is implemented using ASP.NET MVC while the business logic using DDD practices in 4 different variants:

  • classic (no CQRS)
  • CQRS with two NHIbernate relational data stores
  • CQRS with LINQ to SQL on reporting side
  • CQRS with Event Sourcing on command side
like image 116
Szymon Pobiega Avatar answered Nov 19 '22 15:11

Szymon Pobiega


Cqrs makes the web project much easier. On the get site, all the queries will look like "select * from table where id = @id"). For those simple queries, you won't need an orm like NHiberante. You don't have to use a sql database, and when you will, you can serialize your object to the database table, or use a naming convention. You can still query the read database by NHibernate, but you won't get any advantage from it, because all your queries will be the same.

public class Controller
{
  public ActionResult Get(Guid id)
  {
     var viewModel = reportingDatabase.Get(id);
     return View(viewmodel);
  }
}

On the command side, the controllers will look like this:

public class Controller
{
  public ActionResult Post(SomeForm form)
  {
    // do validation
    var command = new SomeCommand(form.Property1, form.Property2);
    bus.Send(command);
    return redirecto(something else);
  }
}

The controller just send a message, and it doesn't know where the message goes to and what the result of the message is. The mvc part of this is very simple to program. Cqrs will make writing the web-part of the application very boring, but you can make it more fun by adding some code that helps the user make decisions (optionally returning json used by ajax).

like image 30
Paco Avatar answered Nov 19 '22 14:11

Paco


And take a look at my attempt at http://agrcqrs.codeplex.com, which is ASP.NET MVC + NHibernate

like image 4
Jan Willem B Avatar answered Nov 19 '22 15:11

Jan Willem B