solnic/dm-session — comment on commit cbebe60daf08f9b658b9c5f65c4ed76f56a943d2
File:  line 0
By dkubb:

@solnic The general case solution is to store a reference to the source data, passing it into the mapper's `#load` method, and then comparing the object against the one that is being tracked in the session. If they are different, then the tracked object is dirty and you can persist it.

It sounds expensive, but since you only do it for objects that have already been added to the session as dirty, it won't be too often that the object turns out to actually be clean (this can happen if a value is changed, then changed back to the original value). Other approaches for dirty tracking require that the object know about dirty tracking, and this really isn't an option in our case.

The state machine object that relates to a "Dirty Object" can perform this checking. A state machine is responsible for state transitions, which includes evaluating guard clauses, this is just an example of a guard. If you look at how it's done in DM1 it's actually quite nice and clean.

As for the original source data, we could track the original tuple in the IM. In Veritas the tuple is just a simple object wrapping an Array, with one entry for each value. A single Header object is shared between the Relation and it's Tuple objects. In the future I may be changing a Tuple so that it's just a Struct subclass; it makes the implementation a bit more difficult, but it uses less memory and provides pretty much the API I want anyway.