ObjDiff 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ObjDiff --version 1.2.0
NuGet\Install-Package ObjDiff -Version 1.2.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="ObjDiff" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ObjDiff --version 1.2.0
#r "nuget: ObjDiff, 1.2.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 ObjDiff as a Cake Addin
#addin nuget:?package=ObjDiff&version=1.2.0

// Install ObjDiff as a Cake Tool
#tool nuget:?package=ObjDiff&version=1.2.0

ObjDiff

Build Tests

A C# .NET Standard library that allows to obtain the differences between two objects using reflection.

Optionally, these differences can be used to patch the first object so it becomes equal to the second one. This patching feature is specially useful when updating Entity Framework objects as it allows to update only those values that have really changed.

Installation

Using NuGet package manager console:

Install-Package ObjDiff

Using .NET CLI:

dotnet add package ObjDiff

Features

  • Compares arrays and collections
  • Compares children objects
  • Configuration options to ignore specific elements
  • Patch an object with an obtained set of differences

Current Limitations

This library is still in an early stage of development and so it lacks some features that similar libraries can offer:

  • Only allows to compare objects of the same type
  • Only public properties are compared

The above limitations will be addressed in future versions.

Usage

Obtaining Differences

using ObjDiff;

var object1 = new CustomObject();
var object2 = new CustomObject();

IEnumerable<Difference> differences = ObjDiff.Diff(object1, object2);

The last line can also be expressed through the use of the Diff extension method:

IEnumerable<Difference> differences = object1.Diff(object2);

The Diff method returns a list with all the differences found between object1 and object2 instances. Each one of these differences are represented through an instance of the Difference class:

  public class Difference
  {
      /// Full path of the element were values differ between compared objects.
      public string Path { get; private set; }

      /// Value of the object being compared.
      public object LeftValue { get; private set; }

      /// Value of the object against LeftValue is being compared.
      public object RightValue { get; private set; }
  }

Comparison Options

It is possible to customize how the comparison process will perform:

var differences = object1.Diff(object2, new CompareOptions { MaxDepth = 10 });

The available options are:

public class CompareOptions
{
    /// When comparing arrays/collections, items must be in the same order. Default value is true.
    public bool CollectionsSameOrder { get; set; }

    /// Ignore properties marked with any of the specified attribute names. None set by default.
    public string[] IgnoredAttributes { get; set; }

    /// Ignore properties named with any of the specified values. None set by default.
    public string[] IgnoredProperties { get; set; }

    /// Maximum number of children levels to dive into. Default value is 10.
    public uint MaxDepth { get; set; }    
}

Patching

Once we have the differences between two objects, we can apply them (patch) to the original object being compared. After patching it, both objects should be equivalent.

object1.Patch(differences);
Assert.Equal(object1, object2);

Both Diff and Patch operations can be executed through a simple call:

ObjDiff.MakeEqual(object1, object2);

Or using MakeEqualTo extension method:

object1.MakeEqualTo(object2);
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 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  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.
  • .NETFramework 4.6.2

    • No dependencies.
  • .NETStandard 2.0

    • 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
1.5.0 2,001 1/22/2023
1.4.0 296 1/10/2023
1.3.2 499 7/18/2022
1.3.1 418 6/22/2022
1.3.0 562 4/21/2022
1.2.0 322 8/27/2021
1.1.0 952 6/7/2021

Added method MakeEqual as a shortcut to call Diff followed by Patch.