ChangeTracking 2.0.22

There is a newer version of this package available.
See the version list below for details.
Install-Package ChangeTracking -Version 2.0.22
dotnet add package ChangeTracking --version 2.0.22
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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();
    

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 25,847 10/18/2019
2.2.16 754 10/4/2019
2.2.13 860 9/3/2019
2.2.4 1,150 7/16/2019
2.2.3 2,914 2/5/2019
2.2.2 606 1/30/2019
2.2.1 577 1/28/2019
2.2.0 598 1/24/2019
2.1.2 4,596 11/29/2018
2.1.1 722 11/1/2018
2.1.0 760 10/15/2018
2.0.22 1,425 9/6/2018
2.0.20 726 9/6/2018
2.0.13 1,439 7/18/2018
2.0.9 924 6/21/2018
2.0.6 3,503 5/28/2018
2.0.0 1,914 4/9/2018
2.0.0-Beta 839 1/28/2018
1.0.46 18,070 1/4/2017
1.0.44 939 12/1/2016
1.0.43 1,047 8/30/2016
1.0.38 1,553 10/30/2015
1.0.34 1,436 7/22/2015
1.0.26 1,272 11/11/2014
1.0.23 1,410 11/5/2014
1.0.22 971 10/2/2014
1.0.19 1,013 8/8/2014
1.0.17-beta 907 6/17/2014
1.0.16-beta 907 6/11/2014
1.0.15-beta 909 6/1/2014
0.0.9-alpha 933 5/20/2014
0.0.8-alpha 906 5/20/2014
0.0.7.2-alpha 879 5/14/2014