I am looking into Event Sourcing.
One of the key points is about saving the differences to the event store rather than objects themselves.
This is fine in simple examples. However, I am struggling with the correct way to do this in the, more complex, real world (i.e. you could have an object with 20 properties on being updated, or the incoming updates could be a list).
To my mind, this means one needs to either:
Option 1 (see what's changed)
- Use reflection to get the differences in state between 2 objects
- Serialize that information to Json (in the case of EventStore), or use another object for Marten.
- Have a means of deserializing that information and rebuilding state if needed. .
The downsides are:
- Add extra work to the system (millions of reflections and serializations on potentially complex objects)
- Require you to have a strict way of message handling to rebuild the model. This could also be brittle when the model changes
Option 2 (just assume there's changes)
If a list comes in, rather than comparing, just assume the data coming in are changes and use one Event to store all that info.
Where the downside is:
- Some changes might not happen (e.g. an "update" is the same as the original), so it's not a true reflection of changes (though it is a true reflection of events).
Option 3 (manual comparisons, lots of events)
- Manually compare ClassA.PropA with ClassB.PropA.
- If it's changed, create a PropAChanged event
The downsides appear to be:
- If there are 20 properties on a class, we have a lot of tedious typing.
- Doesn't seem very DRY.
- I guess you may also have to pay more attention to the order of events too.
I'm thinking option 2 is the way to go.
Is this correct? If not, what is the correct way to do this please?