Category Archives: Uncategorized

ORM vs. CQRS/ES throwdown – Part 2 – The Problem

Let’s approach a simple data domain problem that everyone can relate to.  I’m not going to do the e-commerce or blog demos.  This time, we’ll do something slightly different, we’re going to build a simple Product Evaluation website.

To facilitate a fair set of tests and the ability to swap these models (NHibernate, Entity Framework, and CQRS/ES) in and out, this common model will be injected through a Repository interface definition and the Unity IoC (Inversion of Control) container. For those not in the know, IoC allows objects to be built up from components defined in a configuration file somewhere else in the application.

The entities and data access for our application seem fairly simple:  Products and Evaluations.  We shall define these objects initially as interfaces, so that we can use the same Asp.Net MVC front-end project to access our domain.  This is the “O” in our ORM, our object model looks something like this:

One note: the generic IRepository is defined so that later we can add other repositories and be guaranteed that they all implement the same Get, Save, and Delete method signatures.

The approach is typical of an n-tier application with simple CRUD (Create-Read-Update-Delete) actions available in the repository for the database object being sought.  Pay close attention to that: database object.  Later in this series, this model WILL change, and the effects are profound.  I’ll stop there, I don’t want to foreshadow too much.

At this point, using the class designer and adding some comments to our 4 objects, we haven’t written any REAL lines of code, just definitions at this point.  In future posts, I’ll include NDepend metrics for comparisons and reference GitHub source / tag definitions.

My next post will go through the construction of the “R” of our ORM model – the relational database.  After that we’ll do some simple NHibernate mapping. 

ORM vs. CQRS/ES throwdown – Part 1.5 – Definitions

Before I proceed much further into this series, I realize that some readers may not know the abbreviations and terminology being discussed.  Therefore, a quick introduction to some key terms and technologies:

  • Relational Database – A data storage mechanism that utilizes a series of tables to store data that relates to data in other tables.  Typical examples of this include Microsoft Sql Server, Oracle Database, IBM DB2, MySQL, Postgres, Sqlite, Microsoft Access
  • ORM – Object – Relational – Mapping tool, this is a coding tool that assists developers in managing data in a relational database with an object oriented programming language.
  • NHibernate – A freely available tool with an open source license that provides ORM functionality
  • “The” Entity Framework (EF) – An ORM made by Microsoft and distributed with Microsoft .Net 3.5 and 4.0
  • CQRS – Command Query Responsibility Segregation, a data architecture that separates the read and write actions in the database into Commands and Queries.
  • Event Storage (ES) – A storage mechanism technique that functions similarly to double-entry bookkeeping.  This approach allows the application to store a complete history of transactions and store ALL states of our objects, not just the current state.
  • NoSQLNot Only Sql – this is a relatively new class of database technology that focuses on storing objects or documents in a highly optimized data store.  Examples include MongoDb, Cassandra, Memcache, and Redis

I think that’s a good start.  I will do my best to keep up with any additional terms not defined here at the time that I first introduce them.

ORM vs. CQRS/ES throwdown – Next Steps

Over the next week, you should start to see posts on a daily basis from me (a rarity) outlining all of the steps that I have taken in this comparison.

I will be posting my code to my GitHub account in an open source repository, with each step of the process tagged appropriately.

I’m also using this documentation process as a test to determine how much I enjoy blogging, and if its something I should continue in the future… any and all comments are greatly appreciated.

ORM vs. CQRS/ES throwdown – Part 1 – Introduction

For almost 18 months now, I’ve been reading and reviewing the CQRS architectural approach.  I’ve been using a typical “n-tier” ORM-like approach to structuring my web applications since I started programming in this space more than 10 years ago.

Typical n-tier architecture

I met Udi Dahan in the Spring of 2009 at a Philly.Net code camp.  I was speaking on Asp.Net MVC, and he was presenting on some CQRS basics.  At this time, I had no idea what CQRS was, and was focused on my MVC / NHibernate world. Chatting in the speaker’s lounge, he got me more focused on the simple problem of web programming with Asp.Net – we’re only generating strings.  

If all that Asp.Net programming is, all of this event handling, ViewState management, GET/POST processing, Session storage, and application management boils down to strings… it sure seems like an over-complicated problem.  It got me starting to think about simplifying the string-generation problem.

I went to Tech Ed that year, and after listening to Jeffrey Palermo, started to consider the idea of an ‘edge database’.  Why not?  It’s easy enough to spin up a Sql Express instance on a web server… so just set one up and replicate common data on it.  The memory and processor overhead of that Sql instance proved a bit too much in my practice and I scrapped the idea.

Fast forward a bit, and we’re presented with some of the goodies in the new Entity Framework.  We can generate our objects from the database!  Or we can start in code and generate our database!  All of the plumbing and hbm.xml files from my NHibernate days were beginning to melt away.  After listening to some podcasts with Julie Lerman and Scott Hanselman, I was convinced that the simplification I was looking for was Entity Framework coupled with some neat T4 scripts to generate and manage my MVC3 views.

About 4 months ago, I was offered an opportunity to hear Steve Bohlen talk on DDD and CQRS approaches at a Philly Alt.Net meeting.  At this point, CQRS suddenly made sense for me.  The simplification of smaller objects, easier to manage database layers (in some cases no database layer!) and the intrigue of using NoSQL options to store my front end cache was too good to overlook.

CQRS Architecture

That same group invited Greg Young to speak in August.  After reading through some of his examples, and watching his lectures online, I knew I had to see and hear his talk in person.  I was not disappointed… all of my questions about CQRS were answered and I was confident in my ability to define and work within this architecture’s guidelines.

And that brings me to this:  How does CQRS stack up against my tried and true approach of NHibernate / Entity Framework?  I’ve started coding a prototype application that will compare the two approaches and I’ll share those findings in the next few blog posts.

I am going to measure:

  • Relative lines of code on top of the framework
  • Complexity (from nDepend)
  • Testability
  • Maintainability – we’ll change the domain object and see how it affects the rest of the application

This is going to be very interesting.  I hope you find it interesting as well.