Differ.DotNet 2.1.5

dotnet add package Differ.DotNet --version 2.1.5
NuGet\Install-Package Differ.DotNet -Version 2.1.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Differ.DotNet" Version="2.1.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Differ.DotNet --version 2.1.5
#r "nuget: Differ.DotNet, 2.1.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Differ.DotNet as a Cake Addin
#addin nuget:?package=Differ.DotNet&version=2.1.5

// Install Differ.DotNet as a Cake Tool
#tool nuget:?package=Differ.DotNet&version=2.1.5

Differ.DotNet

<p align="center"> <img src="https://raw.githubusercontent.com/maranmaran/differ-dotnet/main/banner.png" /> </p>

Differ.DotNet is a diffing library for custom types and objects.

Get a list of differences in your instances quickly with the flexibility to specify custom property names, what to keep or ignore in your difference and make your change observation features easier.

Installation

NuGet

Usage

Retrieve differences:

record Car(string Model, string Color, int Year);

Car car1 = new Car("Toyota Camry", "Blue", 2022);
Car car2 = new Car("Ford Mustang", "Red", 2023);

IEnumerable<Difference> carDiff = DifferDotNet.Diff(car1, car2);

Output:

[
  {
    "fullPath": "color",
    "fieldPath": "",
    "fieldName": "color",
    "leftValue": "Blue",
    "rightValue": "Red"
  },
  {
    "fullPath": "model",
    "fieldPath": "",
    "fieldName": "model",
    "leftValue": "Toyota Camry",
    "rightValue": "Ford Mustang"
  },
  {
    "fullPath": "year",
    "fieldPath": "",
    "fieldName": "year",
    "leftValue": 2022,
    "rightValue": 2023
  }
]

KeepInDiff attribute

Keep root and child values even if there is no diff

class Car([property:KeepInDiff]string Model);

Car car1 = new Car("Toyota");
Car car2 = new Car("Toyota");

IEnumerable<Difference> carDiff = DifferDotNet.Diff(car1, car2);

Output

[
  {
    "fullPath": "model",
    "fieldPath": "",
    "fieldName": "model",
    "leftValue": "Toyota",
    "rightValue": "Toyota"
  }
]

Keep in diff has optional property IgnoreIfNoSiblingOrChildDiff which will actually ignore the keep attribute if there's no sibling or child diff present rendering it unusable or not desired as it's extra information without some other context.

IgnoreInDiff attribute

Ignores root and child values

class Car([property:IgnoreInDiff]string Model);

Car car1 = new Car("Toyota");
Car car2 = new Car("Toyota");

// Empty diff
IEnumerable<Difference> carDiff = DifferDotNet.Diff(car1, car2);

Keep and Ignore attribute strength:

More nested attributes have bigger strengths than parent ones. This means that if you define IgnoreInDiff on root property, but KeepInDiff child property or reverse, expect that child property attribute to override parent one.

DiffPropertyName attribute

Applies custom names into Difference.CustomFullPath, Difference.CustomFieldPath, Difference.CustomFieldName props

class Car([property:DiffPropertyName("Make")]string Model);

Car car1 = new Car("Toyota");
Car car2 = new Car("Ford");

IEnumerable<Difference> carDiff = DifferDotNet.Diff(car1, car2);

Output

[
  {
    "fullPath": "model",
    "fieldPath": "",
    "fieldName": "model",
    "leftValue": "Toyota",
    "rightValue": "Ford",
    "customFullPath": "Make",
    "customFieldPath": "",
    "customFieldName": "Make"
  }
]

By turning on flag FromPropertyValue differ will attempt to retireve value via reflection treating given name as PATH.

class Car([property:DiffPropertyName("Company", true)]string Model, string Company);

Car car1 = new Car("Supra", "Toyota");
Car car2 = new Car("Corolla", "Toyota");

IEnumerable<Difference> carDiff = DifferDotNet.Diff(car1, car2);

Output

[
  {
    "fullPath": "model",
    "fieldPath": "",
    "fieldName": "model",
    "leftValue": "Toyota",
    "rightValue": "Corolla",
    "customFullPath": "Toyota", <-- taken from Company prop
    "customFieldPath": "",
    "customFieldName": "Toyota" <-- taken from Company prop
  }
]

This can also be nested path like: Company.Name Iterables are not supported. Default return value is Name value of the DiffPropertyName attribute

DiffCollectionId attribute

Switches default index-based diffing to key-value diff. Nested types and values in the collection return their keys which are used to detect changes.

Say we have 3 items in an array and we remove the first item:

With index-based matching

left right
1, car 2, bike
2, bike 3, road
3, road null

Diff will be: car → bike road → null

Because due to removal, items moved in the array and indexes changed.

With key-based matching by defining DiffCollectionId of underlying object:

left right
1, car null
2, bike 2, bike
3, road 3, road

Diff will be: car → null

Full demo

Combine and enjoy:

    public class Car
    {
        public string Model { get; set; }

        [IgnoreInDiff]
        public string Color { get; set; }

        public int Year { get; set; }

        public List<Accessory> Accessories { get; set; }

        [IgnoreInDiff]
        public List<string> Features { get; set; }

        [JsonIgnore]
        [DiffPropertyName("features")]
        public string FeaturesFlat => string.Join(", ", Features);

        public Car(string model, string color, int year)
        {
            Model = model;
            Color = color;
            Year = year;
            Accessories = new List<Accessory>();
            Features = new List<string>();
        }
    }

    public class Accessory
    {
        [KeepInDiff]
        public string Name { get; set; }

        public decimal Price { get; set; }
    }

    Car car1 = new Car("Toyota Camry", "Blue", 2022);
    car1.Accessories.Add(new Accessory { Name = "Floor Mats", Price = 50.99m });
    car1.Accessories.Add(new Accessory { Name = "Roof Rack", Price = 150.99m });
    car1.Features.Add("GPS Navigation");
    car1.Features.Add("Backup Camera");

    Car car2 = new Car("Honda Civic", "Silver", 2023);
    car2.Accessories.Add(new Accessory { Name = "Floor Mats", Price = 80.99m });
    car2.Accessories.Add(new Accessory { Name = "Roof Rack", Price = 200.50m });
    car2.Features.Add("Sunroof");
    car2.Features.Add("Lane Departure Warning");

    IEnumerable<Difference> carDiff = DifferDotNet.Diff(car1, car2);

Output:

[
  {
    "fullPath": "accessories.0.name",
    "fieldPath": "accessories.0",
    "fieldName": "name",
    "leftValue": "Floor Mats",
    "rightValue": "Floor Mats"
  },
  {
    "fullPath": "accessories.0.price",
    "fieldPath": "accessories.0",
    "fieldName": "price",
    "leftValue": 50.99,
    "rightValue": 80.99
  },
  {
    "fullPath": "accessories.1.name",
    "fieldPath": "accessories.1",
    "fieldName": "name",
    "leftValue": "Roof Rack",
    "rightValue": "Roof Rack"
  },
  {
    "fullPath": "accessories.1.price",
    "fieldPath": "accessories.1",
    "fieldName": "price",
    "leftValue": 150.99,
    "rightValue": 200.50
  },
  {
    "fullPath": "featuresFlat",
    "fieldPath": "",
    "fieldName": "featuresFlat",
    "leftValue": "GPS Navigation, Backup Camera",
    "rightValue": "Sunroof, Lane Departure Warning",
    "customFullPath": "features",
    "customFieldPath": "",
    "customFieldName": "features"
  },
  {
    "fullPath": "model",
    "fieldPath": "",
    "fieldName": "model",
    "leftValue": "Toyota Camry",
    "rightValue": "Honda Civic"
  },
  {
    "fullPath": "year",
    "fieldPath": "",
    "fieldName": "year",
    "leftValue": 2022,
    "rightValue": 2023
  }
]

License

See LICENSE.

Copyright (c) 2023 Marko Urh and other authors.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.5 573 7/20/2023
2.1.4 142 7/19/2023
2.1.3 141 7/18/2023
2.1.2 161 7/14/2023
2.1.1 126 7/12/2023
2.1.0 265 6/29/2023
2.0.4-optional-keep 153 6/26/2023
2.0.3 165 6/19/2023
2.0.2 127 6/16/2023
2.0.1 125 6/16/2023
2.0.0 132 6/15/2023
1.0.0 132 6/15/2023