Err.ChangeTracking.SourceGenerator 0.4.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Err.ChangeTracking.SourceGenerator --version 0.4.0
                    
NuGet\Install-Package Err.ChangeTracking.SourceGenerator -Version 0.4.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="Err.ChangeTracking.SourceGenerator" Version="0.4.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Err.ChangeTracking.SourceGenerator" Version="0.4.0" />
                    
Directory.Packages.props
<PackageReference Include="Err.ChangeTracking.SourceGenerator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Err.ChangeTracking.SourceGenerator --version 0.4.0
                    
#r "nuget: Err.ChangeTracking.SourceGenerator, 0.4.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.
#:package Err.ChangeTracking.SourceGenerator@0.4.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Err.ChangeTracking.SourceGenerator&version=0.4.0
                    
Install as a Cake Addin
#tool nuget:?package=Err.ChangeTracking.SourceGenerator&version=0.4.0
                    
Install as a Cake Tool

Err.ChangeTracking Solution (Full Documentation)

NuGet NuGet

A complete, high-performance, AOT-friendly solution to track changes on POCO models โ€” combining runtime efficiency and compile-time generation.


๐Ÿ“š Introduction

Managing changes on POCO models is a common but tedious problem. Manually tracking modifications leads to verbose code, maintenance headaches, and runtime inefficiency.

Err.ChangeTracking provides:

  • A lightweight runtime library.
  • A powerful Roslyn Source Generator to automate everything at compile time.

Result?
โšก Lightning-fast, zero-reflection change tracking ready for Blazor, NativeAOT, Cloud, Web, and Mobile.


๐Ÿšจ Problem Statement

โœ… How to detect if a POCO model has been modified?
โœ… How to rollback changes easily?
โœ… How to track changes without relying on slow reflection or dynamic proxies?

Conventional solutions introduce runtime overhead and are not AOT-friendly.


๐Ÿ›  Manual Implementation Example (Using Only Err.ChangeTracking Runtime)

using System;
using Err.ChangeTracking;

public partial class Person : ITrackable<Person>
{
    private IChangeTracking<Person> _changeTracker;
    public IChangeTracking<Person> GetChangeTracker() => _changeTracker ??= new ChangeTracking<Person>(this);

    private string _firstName;
    public partial string FirstName
    {
        get => _firstName;
        set { _changeTracker?.RecordChange(nameof(FirstName), _firstName, value); _firstName = value; }
    }

    private int _age;
    public partial int Age
    {
        get => _age;
        set { _changeTracker?.RecordChange(nameof(Age), _age, value); _age = value; }
    }
}

Explanation:

  • _changeTracker monitors the original values.
  • RecordChange is called manually inside each setter.
  • IsDirty, Rollback, and AcceptChanges become available.

โœ… Powerful โ€” but โœ‹ very repetitive and error-prone for large models.


Instead of manually writing setters, just annotate your class:

[Trackable]
public partial class Person
{
    public partial string FirstName { get; set; }
    public partial int Age { get; set; }
}

The source generator will auto-generate the backing fields, tracking logic, and setter wrappers.

โœ… Keep your models clean, readable, and efficient.


โœจ How the Generator Works

  • Class must be marked as [Trackable]
  • Class must be partial
  • Public, partial properties are eligible
  • Attributes control behavior:
Attribute Behavior
[TrackOnly] Track this property only (when using TrackingMode.OnlyMarked)
[NotTracked] Exclude this property from tracking
[TrackCollection] Track changes inside List/Dictionary items
TrackingMode.All (default) track all eligible properties
TrackingMode.OnlyMarked Only track [TrackOnly] properties

๐Ÿงช Examples From Unit Tests

Tracking simple properties

var person = new Person { FirstName = "Alice", Age = 30 }.AsTrackable();
person.FirstName = "Bob";

Assert.True(person.GetChangeTracker().IsDirty);
Assert.True(person.GetChangeTracker().HasChanged(x => x.FirstName));

Rollback a single property

person.GetChangeTracker().Rollback(x => x.FirstName);
Assert.Equal("Alice", person.FirstName);

Tracking collections

[Trackable]
public partial class Order
{
    [TrackCollection]
    public partial List<string> Items { get; set; }
}

var order = new Order { Items = new List<string>() }.AsTrackable();
order.Items.AsTrackable().Add("New Item");

Assert.True(order.GetChangeTracker().IsDirty);

TrackingMode OnlyMarked with [TrackOnly]

[Trackable(Mode = TrackingMode.OnlyMarked)]
public partial class Invoice
{
    [TrackOnly]
    public partial string InvoiceNumber { get; set; }

    public partial string Comment { get; set; }
}

var invoice = new Invoice { InvoiceNumber = "INV001", Comment = "Test" }.AsTrackable();

invoice.InvoiceNumber = "INV002";
invoice.Comment = "Changed Comment";

Assert.True(invoice.GetChangeTracker().HasChanged(x => x.InvoiceNumber));
Assert.False(invoice.GetChangeTracker().HasChanged(x => x.Comment));

๐Ÿš€ Typical Use Cases

  • Change tracking in CRUD applications
  • Undo/Redo features
  • Form validation and dirty detection
  • Optimizing entity update operations

๐Ÿ“ฆ Requirements

IMPORTANT: This package uses C# 13 feature Patrial properties and requires either:

  • .NET 9.0 or higher, OR
  • .NET 8.0 with LangVersion set to "preview" in your project file

If you're using .NET 8.0, add the following to your project file:

<PropertyGroup>
    <LangVersion>preview</LangVersion>
</PropertyGroup>

! Without this configuration, the source generator will not work correctly.

๐Ÿ“ฆ Installation

dotnet add package Err.ChangeTracking

dotnet add package Err.ChangeTracking.SourceGenerator

๐Ÿ“‹ License

Licensed under the MIT License.



๐Ÿ™Œ Contributions

Contributions are welcome!
Fork the repository, submit a PR, or open an issue to suggest improvements!


Built with โค๏ธ by ERRADIL

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has 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
0.5.5 152 6/2/2025
0.5.3 151 5/26/2025
0.5.0 149 5/22/2025
0.4.2 150 5/21/2025
0.4.1 153 5/20/2025
0.4.0 169 5/16/2025