ChangeTracking 2.0.22

.NET Standard 2.0 .NET Framework 4.5.2
There is a newer version of this package available.
See the version list below for details.
dotnet add package ChangeTracking --version 2.0.22
NuGet\Install-Package ChangeTracking -Version 2.0.22
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="ChangeTracking" Version="2.0.22" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ChangeTracking --version 2.0.22
#r "nuget: ChangeTracking, 2.0.22"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install ChangeTracking as a Cake Addin
#addin nuget:?package=ChangeTracking&version=2.0.22

// Install ChangeTracking as a Cake Tool
#tool nuget:?package=ChangeTracking&version=2.0.22

Build status NuGet Badge Test status

ChangeTracking

Track changes in your POCO objects, and in your collections. By using Castle Dynamic Proxy to create proxies of your classes at runtime, you can use your objects just like you used to, and just by calling the AsTrackable() extension method, you get automatic change tracking, and cancellation.

All trackable POCOs implement IChangeTrackable<T>, IRevertibleChangeTracking, IChangeTracking, IEditableObject and INotifyPropertyChanged.

And all trackable collections implement IChangeTrackableCollection<T>, IBindingList ICancelAddNew, INotifyCollectionChanged , IList<T>, IList, ICollection<T>, ICollection, IEnumerable<T> and IEnumerable

Installation

PM> Install-Package ChangeTracking

Example

To make an object trackable

using ChangeTracking;
//...
Order order = new Order 
{ 
    Id = 1,
    CustumerNumber = "Test",
    Address = new Address
    {
        AddressId = 1,
        City = "New York"
    },
    OrderDetails = new List<OrderDetail>
    {
        new OrderDetail
        {
            OrderDetailId = 1,
            ItemNo = "Item123"
        },
        new OrderDetail
        {
            OrderDetailId = 2,
            ItemNo = "Item369"
        }
    }
};
Order trackedOrder = order.AsTrackable();

And here is how you get to the tracked info.

var trackable = (IChangeTrackable<Order>)trackedOrder;
// same as
var trackable = trackedOrder.CastToIChangeTrackable();

And here is what's available on trackable.

//Can be Unchanged, Added, Changed, Deleted
ChangeStatus status = trackable.ChangeTrackingStatus;

//Will be true if ChangeTrackingStatus is not Unchanged
bool isChanged = trackable.IsChanged;

//Will retrieve the original value of a property
string originalCustNumber = trackable.GetOriginalValue(o => o.CustumerNumber);

//Will retrieve a copy of the original item
var originalOrder = trackable.GetOriginal();

//Calling RejectChanges will reject all the changes you made, reset all properties to their original values and set ChangeTrackingStatus to Unchanged
trackable.RejectChanges();

//Calling AcceptChanges will accept all the changes you made, clears the original values and set ChangeTrackingStatus to Unchanged
trackable.AcceptChanges();

//If ChangeTrackingStatus is Changed it returns all changed property names, if ChangeTrackingStatus is Added or Deleted it returns all properties
trackable.ChangedProperties();

By default complex properties and collection properties will be tracked (if it can be made trackable) as well. if you do not wish to track them you can set it when creating the trackable.

var trackable = order.AsTrackable(makeComplexPropertiesTrackable: false);

or

var trackable = order.AsTrackable(makeCollectionPropertiesTrackable: false);

And on a collection

var orders = new List<Order>{new Order { Id = 1, CustumerNumber = "Test" } };
IList<Order> trackableOrders = orders.AsTrackable();

And here is how you get to the tracked info.

var trackable = (IChangeTrackableCollection<Order>)trackableOrders;
// Same as
var trackable = trackableOrders.CastToIChangeTrackableCollection();

And here is what's available on trackable.

// Will be true if there are any changed items, added items or deleted items in the collection.
bool isChanged = trackable.IsChanged;

// Will return all items with ChangeTrackingStatus of Unchanged
IEnumerable<Order> unchangedOrders = trackable.UnchangedItems;
// Will return all items that were added to the collection - with ChangeTrackingStatus of Added
IEnumerable<Order> addedOrders = trackable.AddedItems;
// Will return all items with ChangeTrackingStatus of Changed
IEnumerable<Order> changedOrders = trackable.ChangedItems;
// Will return all items that were removed from the collection - with ChangeTrackingStatus of Deleted
IEnumerable<Order> deletedOrders = trackable.DeletedItems;

// Will Accept all the changes in the collection and its items, deleted items will be cleared and all items ChangeTrackingStatus will be Unchanged
trackable.AcceptChanges();

// Will Reject all the changes in the collection and its items, deleted items will be moved back to the collection, added items removed and all items ChangeTrackingStatus will be Unchanged
trackable.RejectChanges();

Requirements and restrictions

  • .net 4.5.2 and above
  • netstandard 2.0
For Plain objects
  • Your class must not be sealed and all members in your class must be public virtual

    public class Order
    {
        public virtual int Id { get; set; }
        public virtual string CustumerNumber { get; set; }
        public virtual Address  Address { get; set; }
        public virtual IList<OrderDetail> OrderDetails { get; set; }
    }
    
For Collections
  • You can only assign the created proxy to one of the implemented interfaces, i.e. ICollection<T>, IList<T> and IBindingList, and the AsTrackable<T>() will choose the correct extennsion method only if called on IList<T>, IList, ICollection<T> and ICollection.

    IList<Order> orders = new List<Order>().AsTrackable();
    
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net452 net46 net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on ChangeTracking:

Package Downloads
SmartLifeLtd

Official Library for Solutions The Library contains the main operations, data, utilities and Base forms for the Solutions. It's build using Visual Studio 2017, C# 7.0 and .Net framework 4.6.1 .

GoLive.Saturn.Auditing

Package Description

Ease.Repository

C#/.Net building blocks supporting implementation of the repository and unit of work patterns compatible with both NoSQL and SQL / session-aware stores. See Ease.Repository.* packages for concrete implementations.

Ease.Repository.AzureTable

C#/.Net building blocks supporting implementation of the repository and unit of work patterns for Azure Table.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.17 43,540 10/18/2019
2.2.16 889 10/4/2019
2.2.13 1,182 9/3/2019
2.2.4 1,360 7/16/2019
2.2.3 3,206 2/5/2019
2.2.2 743 1/30/2019
2.2.1 714 1/28/2019
2.2.0 725 1/24/2019
2.1.2 5,225 11/29/2018
2.1.1 875 11/1/2018
2.1.0 910 10/15/2018
2.0.22 1,698 9/6/2018
2.0.20 890 9/6/2018
2.0.13 1,856 7/18/2018
2.0.9 1,117 6/21/2018
2.0.6 3,836 5/28/2018
2.0.0 2,321 4/9/2018
2.0.0-Beta 950 1/28/2018
1.0.46 26,180 1/4/2017
1.0.44 1,122 12/1/2016
1.0.43 1,241 8/30/2016
1.0.38 1,755 10/30/2015
1.0.34 1,654 7/22/2015
1.0.26 1,504 11/11/2014
1.0.23 1,592 11/5/2014
1.0.22 1,160 10/2/2014
1.0.19 1,215 8/8/2014
1.0.17-beta 1,006 6/17/2014
1.0.16-beta 1,010 6/11/2014
1.0.15-beta 1,009 6/1/2014
0.0.9-alpha 1,036 5/20/2014
0.0.8-alpha 1,012 5/20/2014
0.0.7.2-alpha 983 5/14/2014