Differ.DotNet 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Differ.DotNet --version 2.0.0
NuGet\Install-Package Differ.DotNet -Version 2.0.0
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.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Differ.DotNet --version 2.0.0
#r "nuget: Differ.DotNet, 2.0.0"
#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.0.0

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

Differ.DotNet

DotnetDiffer Banner

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

Get a list of differences of your instances quickly with 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"
  }
]

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 strength 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"
  }
]

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 FeaturesFlag => 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": "featuresFlag",
    "fieldPath": "",
    "fieldName": "featuresFlag",
    "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.

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 578 7/20/2023
2.1.4 144 7/19/2023
2.1.3 143 7/18/2023
2.1.2 163 7/14/2023
2.1.1 128 7/12/2023
2.1.0 268 6/29/2023
2.0.4-optional-keep 154 6/26/2023
2.0.3 168 6/19/2023
2.0.2 129 6/16/2023
2.0.1 127 6/16/2023
2.0.0 134 6/15/2023
1.0.0 135 6/15/2023