May 19 2010

WCF Data Services: A perfect fit for our CQRS implementation

Category: CQRSfossmo @ 15:47

When we started implementing CQRS architecture in the project I'm working on at the moment, we used NHibernate to get the data from the database on the query side and to the view model in the GUI. This worked fine, but it took a bit of time to implement what was needed to get the data through “all” the layers and to the GUI. It was tedious and error prone.

We started to look for other ways to do this. We considered using plain SQL through ADO.NET, but we wanted something that was simpler and faster to implement. By using ADO.NET we still had to create web services and do a bit of mapping. WCF Data Services ended up being our “savior”.

A quick update on WCF Data Services

WCF Data Services is built upon the open data protocol (OData), and OData is built upon the atompub protocol. OData is developed at Microsoft, but published as a open standard. You can read more about it at www.odata.org. I think OData is very exciting, and there are already other languages supporting this protocol besides .NET. To mention some; PHP and Java.
A cool thing; If a data store have a LINQ provider, it’s possible to expose the data through the OData protocol using WCF Data Services in few simple steps. If I’m not mistaken, there is a provider for db4o and a provider under development for MongoDB.

How we did it

image

To set up WCF DS over the read database, the only thing we had to do, was to create the table(s) we wanted exposed through WCF DS. Add a EDMX model to a empty ASP.NET web project, and finally add a WCF Data Service to the web project and added the entity set as a datasource (We also added some security, but when it comes to security in the OData protocol, you’re on your own. OData doesn’t say anything about how you should do this).

 image

So, to fetch data into the view model in the presentation layer, we simply use a LINQ query to get the data we want. DataServiceContext is one of the main actors in this process. The result from the query is added to the presentation model. WCF DS comes with a object-to-object mapper that makes the job mapping to the presentation model very simple. You can set it up to map all the properties in the presentation model, and throw an exception if a property is missing, or set it up to ignore missing properties.
A small code example:

DataServiceContext context = new DataServiceContext(...);
context.IgnoreMissingProperties = true;

The OData protocol gives us a lot of stuff for free; like getting the top ten records, paging, sorting, and other stuff you expect when querying a relational database. And this is just some of the abilities you get with WCF DS. Read about the conventions at http://www.odata.org/developers/protocols/uri-conventions

This has simplified the process of adding new view models to the query side and have made us more productive.

Although we only have implemented this on the query side, I see no problems using the same approach on the command side.

Tags:

Currently rated 3.6 by 7 people

  • Currently 3.571429/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5