Project YakShayQRS : another CQRS evolution
Introduction
TL;DR: I managed to minimize the CQRS overhead even further.
Over the years, I have been trying to minimize CQRS in several iterations, even releasing a framework and a lib in doing so. Yet, I have still not been satisfied by the approach I reached. This time I once again am pretty satisfied with the way things turned out, and they actually seem to require even way less overhead...
How it works
The concept is quite simple: I use a generic message class to implement messaging. Next to this I use the virtual keyword:
- A class can contain virtual properties; these properties define the unique key of the instance. (f.e. an "Account" class has a "protected virtual string AccountId {get;set;}").
- When invoking a message on a class type, an instance is loaded where the unique key is loaded based on the match between the message parameters and the classes' virtual properties. A message only gets invoked if it contains all the virtual properties from the class.
- In order to alter state, one should use a virtual method.
- One can only send messages targetting non-virtual methods to a class instance.
- Non-virtual methods should never alter state, but instead call a virtual method that alters the state...
- When rebuilding state based on past events, only the messages targetting the virtual methods are invoked to rebuild the state; no messages are emitted.
Maybe an example can make it more clear
As usual, the full source can be found over at github.
Conclusion
This is yet another evolutionary approach to CQRS, and it feels pretty neat (event though the same things applied to the previous versions. Let me know what you think !