A Trip to CQRS – New Views

This is part 5 of 5. The entire series is here:

  1. Intro
  2. Commands
  3. Events
  4. Authoritative Events
  5. New Views

We finally have a reasonably flexible architecture to play with. We’ve separated our Commands, we’re storing our Events and we’re using them to build our domain model. Let’s see what else we can do, shall we?

What if we would like to show a history of the last 20 changes made to the system on the front page? Prior to our refactoring that would have introduced quite a lot of problems. In fact, if you run the demo system (or come see me the next time I do this talk) you’ll see that we’ve had the service layer and UI ready for it, but since there was no nice way of implementing it, it’s been left as a hard-coded dummy.

Use those Events

But in our new model, this is actually a piece of cake! We just introduce a new Event Handler that subscribes to all events that are considered as “changes we’d like to show on the front page”, and use it to update a HistoryItem table in the database:

[github file=”/andlju/hotel-admin/blob/history-view/src/HotelAdmin.Service/EventHandlers/HistoryItemEventHandlers.cs” start_line=”8″ end_line=”87″]

Any change from now on will also be passed on to the new view and will show up on the front page. Pretty sweet, isn’t it?

Drop it like it’s hot

Yes. Sweet. But we can do one better.

You know that database of yours? Drop it. Yes. You heard me. DROP it! (But let’s keep the Event Store, ok?)

Remember that we are now Event Sourced? And what is it that you do in an Event Sourced system when your view model changes? You reset it and you replay your events, and that is exactly what we’re going to do too:

[github file=”/andlju/hotel-admin/blob/history-view/src/HotelAdmin.Loader/Events/EventReloader.cs” start_line=”7″ end_line=”17″]

And Hey Presto! We have our old database back, now with a complete set of HistoryItems since the day we started logging. Now THAT is sweet..!

Summary

I cannot stress this enough. This is NOT the way you do CQRS on .NET. It is most likely not even a good idea for you to even think about doing this. But, given the right circumstances and a similar set of problems that we faced. Well, I’ve certainly enjoyed it and the system is proving to be fairly flexible.

If you want to hear more, let me know! I’m happy to share and I would be MORE than happy to hear your thoughts on this! Either pop a comment on one of the posts, or engage me on Twitter (@CodingInsomnia as usual).



2 responses to “A Trip to CQRS – New Views”

  1. Highly intrigued to hear a fleshed out opinion on your last comment – “I cannot stress this enough. This is NOT the way you do CQRS on .NET.” Would you care to elaborate more?
    – Regards, Matt

    Like

    1. Yes. Basically, I don’t want anyone to skim this article, copy the code and start doing the same thing without understanding the reasoning I had when doing it. At least I think it only makes sense to do it this way when you have a similar set of constraints and really need that event log (as I did).

      In a green-field scenario, you would most likely gain a lot more by following a DDD-centric approach (something like Mark Nijhof’s Fohjin sample, or Jonathan Oliver’s Common Domain).

      Does that make sense?

      Like

Leave a comment

About Me

Consultant, Solution Architect, Developer.

Do note that this blog is very, very old. Please consider that before you follow any of the advice in here!

Newsletter