#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>, IBindingListICancelAddNew, IList<T>, IList, ICollection<T>, ICollection, IEnumerable<T> and IEnumerable
PM> Install-Package ChangeTracking
###To make an object trackable
usingChangeTracking;//...Orderorder=newOrder{Id=1,CustumerNumber="Test",Address=newAddress{AddressId=1,City="New York"},OrderDetails=newList<OrderDetail>{newOrderDetail{OrderDetailId=1,ItemNo="Item123"},newOrderDetail{OrderDetailId=2,ItemNo="Item369"}}};OrdertrackedOrder=order.AsTrackable();And here is how you get to the tracked info.
vartrackable=(IChangeTrackable<Order>)trackedOrder;// same asvartrackable=trackedOrder.CastToIChangeTrackable();And here is what's available on trackable.
//Can be Unchanged, Added, Changed, DeletedChangeStatusstatus=trackable.ChangeTrackingStatus;//Will be true if ChangeTrackingStatus is not UnchangedboolisChanged=trackable.IsChanged;//Will retrieve the original value of a propertystringoriginalCustNumber=trackable.GetOriginalValue(o =>o.CustumerNumber);//Will retrieve a copy of the original itemvaroriginalOrder=trackable.GetOriginal();//Calling RejectChanges will reject all the changes you made, reset all properties to their original values and set ChangeTrackingStatus to Unchangedtrackable.RejectChanges();//Calling AcceptChanges will accept all the changes you made, clears the original values and set ChangeTrackingStatus to Unchangedtrackable.AcceptChanges();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.
vartrackable=order.AsTrackable(makeComplexPropertiesTrackable:false);or
vartrackable=order.AsTrackable(makeCollectionPropertiesTrackable:false);###And on a collection
varorders=newList<Order>{newOrder{Id=1,CustumerNumber="Test"}};IList<Order>trackableOrders=orders.AsTrackable();And here is how you get to the tracked info.
vartrackable=(IChangeTrackableCollection<Order>)trackableOrders;// Same asvartrackable=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.boolisChanged=trackable.IsChanged;// Will return all items with ChangeTrackingStatus of UnchangedIEnumerable<Order>unchangedOrders=trackable.UnchangedItems;// Will return all items that were added to the collection - with ChangeTrackingStatus of AddedIEnumerable<Order>addedOrders=trackable.AddedItems;// Will return all items with ChangeTrackingStatus of ChangedIEnumerable<Order>changedOrders=trackable.ChangedItems;// Will return all items that were removed from the collection - with ChangeTrackingStatus of DeletedIEnumerable<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 Unchangedtrackable.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 Unchangedtrackable.RejectChanges();- .net 4 and above
#####For Plain objects
Your class must not be
sealedand all members in your class must bepublic virtualpublicclassOrder{publicvirtualintId{get;set;}publicvirtualstringCustumerNumber{get;set;}publicvirtualAddressAddress{get;set;}publicvirtualIList<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>andIBindingList, and theAsTrackable<T>()will choose the correct extennsion method only if called onIList<T>,IList,ICollection<T>andICollection.IList<Order>orders=newList<Order>().AsTrackable();