Skip to content

1.0 StructuredDiff

Latest

Choose a tag to compare

@ax0l0tl ax0l0tl released this 30 Nov 14:40
· 3 commits to main since this release
651e746

Bluehands.StructuralDiff 1.0.0

First release of Bluehands.StructuralDiff: source-generated structural comparisons with typed property paths for immutable .NET models.

The package ships its runtime and source generator. It has no dependency on the event store, the command layer or any persistence provider, so you can use it in any .NET project.

dotnet add package Bluehands.StructuralDiff

Requires .NET 10. Generated code requires C# 12 or later. The generator targets .NET Standard 2.0 and references Roslyn 4.11, so it loads in older compilers too.

Quick start

using Bluehands.StructuralDiff;

public record Address(string City);
public record Customer(string Name, Address Address, int Version);

[Diff(typeof(Customer), nameof(Customer.Version))]
public static partial class CustomerComparisons;

var before = new Customer("Alice", new("Paris"), 1);
var after  = new Customer("Alice", new("Berlin"), 2);

var differences = before.Diff(after);
// One difference: Address.City, old "Paris", new "Berlin"
bool changed = before.HasDifferences(after);   // true

No attributes or partial modifiers are needed on your models.

Highlights

  • Typed property paths. Every Difference has a PropertyPath, an OldValue, a NewValue and a kind (Added, Removed or Changed).
  • HasDifferences. Stops at the first difference without building a result list.
  • Collections. Dictionaries are matched by key, sets by membership, and lists by index or by an explicit key with [Diff(typeof(OrderLine), MatchBy = nameof(OrderLine.Id))].
  • Polymorphism. Closed concrete subclasses and interface implementations in the consumer assembly are discovered automatically.
  • Ignore properties and paths. Exclude properties at generation time, or exact paths at runtime.
  • TreatAsValue compares a domain type, such as an ID, as one atomic value.
  • Custom comparisons. Provide a static Diff overload for a type in the comparison class.
  • Diagnostics. SDIFF001–SDIFF007 report invalid declarations at compile time.

Built to not throw on data

Diffs don't fail because of the data they compare. Duplicate or null keys, null elements, mismatched key comparers and unknown runtime subtypes take a defined fallback and still return a result. Set DiffSettings.OnFallback to log them. This makes the package safe in reactive pipelines and event replay, where one malformed state should not abort everything.

Performance

Unchanged parts that share a reference are skipped, paths are built only for recorded differences, and comparing unchanged states allocates nothing in the common case. See the benchmark README for measurements.

Limitations

  • Structs, including record structs, are not traversed; they are compared by equality.
  • Unknown runtime subtypes from other assemblies are compared with Equals only.
  • Set elements are not traversed, and keyed collections ignore order.
  • Cyclic object graphs, glob patterns, patch generation and automatic redaction are not included.
  • Results hold references to the original values. They can contain secrets, so filter or format them before logging.

Full documentation: package README