C# || How To Add Simple Object Change Tracking To Track Changes Using C#

The following is a module with functions which demonstrates how to add simple object change tracking to track changes made to an object using C#.

Contents

1. Overview
2. Basic Usage
3. Accept Changes
4. Reject Changes
5. Ignore Tracking For Property
6. Notify Property Changed
7. Utils Namespace
8. More Examples


1. Overview

The following is a simple abstract change tracker class which implements INotifyPropertyChanged and IRevertibleChangeTracking which provides functionality to track object changes, and to ‘accept’ or ‘revert’ changes.

To use this class, simply inherit the abstract class, and your all set!

Note: Don’t forget to include the ‘Utils Namespace‘ before running the examples!


2. Basic Usage

The example below demonstrates the use of ChangeTracker.BeginChanges to start tracking object changes, as well as ChangeTracker.GetChanges to get the changes made to an object.

The following example demonstrates the basic usage of adding change tracking to an object, and making and getting changes.


3. Accept Changes

The example below demonstrates the use of ChangeTracker.AcceptChanges to accept modification changes made to an object.

This function commits all the changes made to the object since either ChangeTracker.BeginChanges was called, or since ChangeTracker.AcceptChanges was last called.

When the accept function is called, all the changes made to the object up to that point will be marked as the current ‘source of truth’ for change tracking.


4. Reject Changes

The example below demonstrates the use of ChangeTracker.RejectChanges to reject modification changes made to an object.

This function rejects all the changes made to the object since either ChangeTracker.BeginChanges was called, or since ChangeTracker.AcceptChanges was last called.

When the reject function is called, all the changes made to the object up to that point reverts back to the objects state before modifications initially began or modifications was last accepted.


5. Ignore Tracking For Property

The example below demonstrates the use of ChangeTracker.ChangeTrackerIgnore attribute to mark a specific property to be ignored from change tracking.


6. Notify Property Changed

The example below demonstrates the use of ChangeTracker.NotifyPropertyChanged to fire the PropertyChanged event notifying that the specified property value has changed.

In the class declaration, simply add ChangeTracker.NotifyPropertyChanged to the properties you wish to notify changes, and add a PropertyChanged event handler function to receive the notifications.


7. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


8. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Was this article helpful?
👍 YesNo

Leave a Reply