As I recently blogged, the project I’m on has recently decided to move to CQRS (Command Query Responsibility Segregation). We’re going to use nServiceBus as a message bus that let’s us separate our system into 2 “channels”: The “Read side” and the “Write side” (aka, the “Query side” and the “Command Side”).
This decision has been the result of several “Light Bulb Moments”, in which various ones of us had a flash of insight that helped us see how an architecture that at first sounded wierd and unorthodox would actually solve a number of problems and help us tremendously.
I’ve decided to share here one of those Light Bulb Moments in raw form. Here’s the text of an email I sent to two other architects on our team (over the weekend, from my own account … we talk about this stuff all the time because we love it). It expresses well many of the reasons we made the move (although I understand more about CQRS at this point and would tweak a few details). (Note: Names changed to protect the guilty).
The Email…
Guys,
I’m seeing the opportunity to do something truly awesome here. It is based on the CQS reading I’ve been doing while thinking about what our “dto’s” or “commands” or etc. should look like.
I have created, worked with, and seen first hand the power of an OLAP database for read operations. It really is unbelievable in terms of the freedom it gives someone looking at the data. And it lets reads be very fast. But a lot of projects I’ve been on have said, “Let’s build the transactional system first. It is so obviously core to our business that we need it, and we need it yesterday. Once we get that done, we can think about maybe doing OLAP.”
But the way people are approaching CQS as a architectural concept these days, we have the opportunity to do both at the same time. It should help us get to the finish line faster, with screaming fast software and high scalability.
And it’s not that big of a change from what we’re doing now. It boils down to this:
1. We make the “flat view models” you guys are working on. They are designed to serve the view that they populate, and nothing else.
2. We express our edits to the domain in terms of “Commands”. These are merely Declarative … you look at one and it intuitively makes sense.
3. Our Domain Objects accept those Commands and process them. Our Domain Objects apply rules to decide whether or not a Command is valid. The Domain Objects have complete authority over accepting or rejecting an Edit Command.
4. Once the Edit Command is accepted by the Domain Objects, it is “applied”.
Now, right now, you’re both saying, “No shit, that’s what I said on Friday.” Yes, but let’s take stock of where this puts us, and see what else it allows us to do.
5. Since those “flat view models” don’t enforce any important business rules, they don’t have to come from our Domain Objects. (They can STILL come from NHibernate if that’s important or helpful, but they don’t have to come from our Domain Objects). Remember, our Domain Objects are in charge of *writing* all updates. Therefore, the written data can include calculated fields and anything else necessary to ensure that what comes back in on the read side is valid and complete. Complete domain integrity is maintained by the Domain Objects, so Reading is simplified. Needing a bunch of business logic on read has some challenges to it, plus I don’t think we have very many (maybe not any) kinds of calculated fields that would really require a full domain object.
6. We *are* still talking about NHibernate pulling the data that ultimately goes into our View Models. So there are probably some “DTO’s” that are *also* mapped to the same NHibernate tables that our Domain Objects write to. But those DTO’s can be “screen-shaped” (more accurately, “task-shaped”, since we want to include web services and other users of our system besides just the web-based human interface).
7. Now, the domain no longer needs many (possibly any) getters or setters.
8. Every single Edit Command can cause 2 things to happen: 1) our normalized OLTP database can get updated by our Domain Objects with the new data. 2) The very same Edit Command can get queued somewhere else to cause an update to our OLAP database for read access. We can essentially get an OLAP database that doesn’t need ETL … it gets updated from our Edit Commands and only lags a few seconds behind our OLTP database.
9. The Edit Commands also make it easy for us to have *MANY* copies of the readable OLAP database. We can update 3 databases as easily as one. Now we can load balance between them, and they’re equivalent.
10. We don’t actually need the fix Billy added to submit disabled controls. After all, we *know* those values didn’t change. Why should we need them on a Post? Our Edit Commands can be as sparse as what the user actually changed. (This is a minor thing, but still worth mentioning).
11. Here’s one of the main benefits of the whole thing: Once we get to this point, when we make a new screen, we make a DTO for it and a View Model for it. *Both* are custom designed to fit the screen itself. They will be coupled to the purposes of that screen because they need to be … this is good coupling. However, that screen will not exert *any pressure whatsoever* on our Domain Model. Our Domain Model will simply be exactly what it needs to be to express the logic of the domain. Think about how much easier things will be for us than they are right now. Now, we have to have a Domain Object Graph, and a parallel DTO object graph, and (soon) a View Model that gets mapped from the DTO object graph. Keeping the parallel Domain Object and DTO in sync has proven to be something we invest a lot of time in. They were drifting apart before I added the ApplyEdits() stuff. I then added the Interfaces that sometimes have 4 or 5 generic types riding along. Sam went further with it and has cases with 8 or 12 generic types, including multiple levels of nesting. We’re working too hard here. *IF you LOVE THE SMELL OF DELETED BITS IN THE MORNING”*, then you are going to get enjoyment out of moving to an approach like this.
Normally, doing something to fix problems you are having requires some extra work you hadn’t anticipated, and is a bit of a setback, though necessary. In this case, the fix for some of the problems we’re running into actually opens up whole new vistas of possibility, and these opportunities basically come for free after applying the correct fix.