AnyDiff
AnyDiff copied to clipboard
Add Ability to Diff Against NULL?
Firstly, thanks for the great library, I've just found it yesterday and have found it very useful and configurable so far!
One use case I have that it doesn't seem to cater for however is the ability for one (or arguably both) of the objects to be diff'ed to be null
. I am using AnyDiff to identify differences between a source and target object for both insert and update operations against my data store. In the case of inserts, my 'source' object will be null
and it would be extremely useful for AnyDiff.Diff() to return an ICollection<Differences>
which includes all fields marked for comparison in the target object and their values in RightValue
and I suppose null
(or the most suitable 'empty' value given the fields data type) in LeftValue
.
I cannot see anyway of achieving this currently unless I am missing something? I've tried markiny my object for comparison as nullable via T?
in the types method call below:
ICollection<Difference> differences = AnyDiff.AnyDiff.Diff<User?>(
existingUser,
currentUser,
ComparisonOptions.All | ComparisonOptions.AllowCollectionsToBeOutOfOrder,
new string[] { "Id", "CreatedAt", "UpdatedAt" }
);
but this results in:
System.NullReferenceException: Object reference not set to an instance of an object.
Further stack trace:
System.NullReferenceException: Object reference not set to an instance of an object.
at TypeSupport.Extensions.<fd5fea14-b6d2-4046-8d81-b8115045ec3c>ObjectExtensions.GetProperties(Object obj, <4d7a44c8-b5f5-44bf-8471-f0980838d61e>PropertyOptions options)
at AnyDiff.DiffProvider.RecurseProperties(Object left, Object right, Object parent, List`1 differences, Int32 currentDepth, Int32 maxDepth, <37c2b6f9-1728-45df-bb6e-473d4690fe19>ObjectHashcodeMap objectTree, String path, ComparisonOptions comparisonOptions, ICollection`1 propertiesToExcludeOrInclude, DiffOptions diffOptions)
at AnyDiff.DiffProvider.ComputeDiff(Object left, Object right, Int32 maxDepth, ComparisonOptions comparisonOptions, DiffOptions diffOptions, String[] propertiesToExcludeOrInclude)
at AnyDiff.AnyDiff.Diff[T](T left, T right, ComparisonOptions comparisonOptions, String[] propertiesToExcludeOrInclude)
My best idea for a workaround currently would be to create additional classes with nullable and/or sane default fields e.g. BlankUser
, enabling the AllowCompareDifferentObjects
option and passing this in as my source in these scenarios.
If however, AnyDiff could allow for a null
left value however, that would be great?