GitXplorerGitXplorer
r

AnyDiff

public
154 stars
17 forks
11 issues

Commits

List of commits on branch master.
Unverified
b8d5c7e284259c40d345ad45c78f8c51d1beca31

fix #27

MMohammadHadi2031 committed 3 months ago
Unverified
d03f6858eb86fa79e3169b410aadedb8b9dc8fd5

add array index in diff path

MMohammadHadi2031 committed 3 years ago
Unverified
fe9c29f847650acff251b89f8ebecb791e7556ea

Changed to MIT license to be less restrictive on commercial use.

rreplaysMike committed 6 months ago
Unverified
9c63f31ef0df28dd882b9357bddada4ff2b05eb7

strong names nuget package assembly

rreplaysMike committed 3 years ago
Unverified
82fb8632efc2473fd53c830073dbbf3d3999c8ac

nuget package updates

rreplaysMike committed 3 years ago
Unverified
caf39f0c3e58fdcfcb9d0e3a7b279ac9f39cfbfe

Upgrades TypeSupport to 1.1.4

rreplaysMike committed 4 years ago

README

The README file for this repository.

AnyDiff

nuget nuget Build status Codacy Badge Codacy Badge

A CSharp (C#) diff library that allows you to diff two objects and get a list of the differences back.

Description

AnyDiff works with complex objects of any type, and is great for performing changeset tracking, auditing, or anything else that might require comparing differences between complex objects. Great when combined with AnyClone, as it lets you take a snapshot of the current state of an object and then compare it in the future for changes to it.

It can even do this for objects that are of different types, though the results may vary depending on how different they are.

Installation

Install AnyDiff from the Package Manager Console:

PM> Install-Package AnyDiff

Usage

Comparing two objects with no differences:

using AnyDiff;

var object1 = new MyComplexObject(1, "A string");
var object2 = new MyComplexObject(1, "A string");
var diff = AnyDiff.Diff(object1, object2);
Assert.AreEqual(diff.Count, 0);

Alternate extension syntax is also available (we will use this in further examples):

using AnyDiff.Extensions;

var object1 = new MyComplexObject(1, "A string");
var object2 = new MyComplexObject(1, "A string");
var diff = object1.Diff(object2);
Assert.AreEqual(diff.Count, 0);

Comparing two objects with a single expected change:

var object1 = new MyComplexObject(1, "A string");
var object2 = new MyComplexObject(1, "A different string");
var diff = object1.Diff(object2);
Assert.AreEqual(diff.Count, 1);

Comparing objects using custom Type Converters for proper delta detection:

public class MyComplexObject
{
  public int Id { get; private set; }

  /// <summary>
  /// Convert a formatted string as a TimeSpan
  /// </summary>
  [TypeConverter(typeof(TimeSpanConverter))]
  public string StartTime { get; set; }

  public TypeConverterObject(int id, string startTime)
  {
    Id = id;
    StartTime = startTime;
  }
}

var object1 = new MyComplexObject(1, "04:00:00");
var object2 = new MyComplexObject(1, "04:05:00");
var diff = object1.Diff(object2);
Assert.AreEqual(diff.Count, 1);
Assert.AreEqual(TimeSpan.FromMinutes(5), diff.First().Delta); // difference of 5 minutes

Ignoring Properties

Anydiff will ignore fields and properties decorated using attributes: [IgnoreDataMember], [NonSerialized], and [JsonIgnore]. In addition, you can specify properties to ignore using expression syntax. See Ignoring Properties and Fields for more details.

Ignoring by properties explicitly by passing a list of properties via expressions:

var object1 = new MyComplexObject(1, "A string", true);
var object2 = new MyComplexObject(2, "A different string", true);
var diff = object1.Diff(object2, x => x.Id, x => x.Name);
Assert.AreEqual(diff.Count, 0);

Diff specified properties only

AnyDiff also supports processing of specific properties if you don't want to diff the entire object. This works using the same syntax as ignoring properties but passing a different ComparisonOptions. In the example below, only the properties Id and Name will be compared.

var object1 = new MyComplexObject(1, "A string", true);
var object2 = new MyComplexObject(2, "A different string", true);
var diff = object1.Diff(object2, ComparisonOptions.All | ComparisonOptions.IncludeList, x => x.Id, x => x.Name);
Assert.AreEqual(diff.Count, 0);

Comparing Unordered lists

If you wish to perform a diff that ignores the ordering of data in a collection/list, you can specify that behavior with a ComparisonOptions AllowCollectionsToBeOutOfOrder flag as well as the AllowEqualsOverride, as seen below.

var list1 = new List<int> { 1, 2, 3 };
var list2 = new List<int> { 1, 3, 2 };
var diff = list1.Diff(list2, ComparisonOptions.All | ComparisonOptions.AllowCollectionsToBeOutOfOrder | ComparisonOptions.AllowEqualsOverride);
Assert.AreEqual(diff.Count, 0);

Analyzing results

Viewing the results of a diff:

var diff = object1.Diff(object2);

foreach(var difference in diff)
{
  Console.Write($"Index: {difference.ArrayIndex}"); // when array elements differ in value
  Console.Write($"Delta: {difference.Delta}"); // when numbers, Dates, Timespans, strings differ in value
  Console.Write($"Left: {difference.LeftValue}"); // the left value being compared
  Console.Write($"Right: {difference.RightValue}"); // the right value being compared
  Console.Write($"Property name: {difference.Property}"); // the name of the field/property
  Console.Write($"Property type: {difference.PropertyType}"); // the type of the field/property
}

Scenarios supported

  • [x] Circular references
  • [x] Using TypeConverters to understand data types
  • [x] Deltas on strings, DateTimes, TimeSpans, numeric types
  • [x] Comparing arrays, collections, custom collections, dictionaries, hashtables
  • [x] Comparing collections with different ordering
  • [x] Complex objects, deep type inspection
  • [x] Entity Framework objects
  • [x] IEquatable support

Using with other libraries

Comparing the difference between the same object at different states, using AnyClone

using AnyDiff.Extensions;
using AnyClone;

var object1 = new MyComplexObject(1, "A string");
var object1Snapshot = object1.Clone();

var diff = object1.Diff(object1Snapshot);
Assert.AreEqual(diff.Count, 0);

// change something anywhere in the object tree
object1.Name = "A different string";

diff = object1.Diff(object1Snapshot);
Assert.AreEqual(diff.Count, 1);