I’ve always used native SQL for my applications. Approximately a year or so ago I finally decided to give NHibernate a look and see what all the hype was about. I gave it a test run on a new project as there is really nothing like real world requirements to show you what a tool is really made of.
It started off slowly, but was really enjoying the easy of which you could do inserts and updates. Even selects where pretty easy. At least at first.
As the system started to grow, the pain started. The queries were relatively straight forward, but the object graph needed to pull all the data together for a simple nested list was becoming convoluted to say the least. At once stage I think I spend almost a day trying to figure out a query, however at the time I was still learning so I knew there would be some pain but it was starting to become ridiculous.
Let me say though, I’m not entirely blaming NHibernate (or ORMS eg. EF) for the problems. The data interactions that I was trying to perform were pretty intense, with complex hierarchical relationships, but I knew there had to be a better way.
I started to switch most of the queries over to using session.CreateSqlQuery(sql) and using the AliasToBeanTransformer to project the query into a DTO that more closely represented how the data was to be displayed on the page.
It was at this time that CQRS was starting to gain some traction and it was all starting to make a lot of sense.
How you model the data is usually not how you need to display the data.
I couldn’t believe how simple everything was after separating the queries from the commands. It was definitely an “aha” moment!
I am currently not implementing CQRS in full (event sourcing, table per model, separate read store etc) but I think it is a great compromise.
I have started to put together a sample application of how I am doing Web Applications using CQRS-Lite and I will endeavour to blog about the details in the upcoming weeks. This will also include details on the new Micro-ORM PetaPoco that I have been helping Brad out on, and how much easier it has made things!