This is part 5 of 5. The entire series is here:
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:
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:
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..!
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).