1

Resolved

LightDataAccess.ObjectsChangeTracker.GetChanges overloaded version

description

It would be nice to have overloaded version of GetChanges method, which gives changes between two different objects of the same type.
It's not always convinient/possible to call RegisterObject and later get its changes.
Often I have just two different "snapshot" objects of the same type.
Of course I can write the following code:
 
var tracker = new ObjectsChangeTracker();
var mapper = ObjectMapperManager.DefaultInstance.GetMapper<AAA, AAA>();
var tmp = mapper.Map(originalObj);
tracker.RegisterObject(tmp);
mapper.Map(currentObj, tmp);
var changes = tracker.GetChanges(tmp);
 
but it's much easier and better from performance standpoint to write it in the following way:
 
var tracker = new ObjectsChangeTracker();
var changes = tracker.GetChanges(originalObj, currentObj);
 
The implementation of overloaded GetChanges method is below:
 
public TrackingMember[] GetChanges(object originalObj, object currentObj)
{
  if (originalObj == null || currentObj == null || originalObj.GetType() != currentObj.GetType())
    return null;
  var originalValues = GetObjectMembers(originalObj);
  var currentValues  = GetObjectMembers(currentObj);
  return currentValues
    .Where(
      (current, idx) =>
      {
        var original = originalValues[idx];
        return
          ((original.value == null) != (current.value == null))
          ||
          (original.value != null && !original.value.Equals(current.value));
      }
    )
    .ToArray();
}

comments