como-controlar-cambios-en-entidades-en-c-con-trackerdog

How to track changes in C# entities with TrackerDog

  • 3 min

TrackerDog is a library for .NET that allows us to easily detect changes in entities, so we know which properties have been modified during the use of an instance.

When using an ORM like Entity Framework or NHibernate, the ORM itself provides change tracking, so it can update only the values that have actually changed in the database.

However, if we use a micro ORM like Dapper, or even without using a database at all, we often encounter situations where it is necessary to record changes in a series of objects.

Implementing change tracking requires a large amount of repetitive code, or using inefficient solutions. There are several possible libraries to help us with change tracking. TrackerDog is one of the most powerful and flexible, making an effort not to compromise efficiency.

TrackerDog generates Proxies for our entities, i.e., objects that inherit all the properties of the original, adding additional functionality. In this way, these Proxies are compatible with almost all code where we use the original classes.

TrackerDog is compatible with .NET Framework, .NET Core, and Xamarin. It is Open Source and the code is available at https://github.com/mfidemraizer/trackerdog.

Installing TrackerDog

TrackerDog is available via a Nuget package, which we can install from the package manager or through its command console by doing

Install-Package TrackerDog -Version 2.2.4
Copied!

Using TrackerDog

As mentioned, TrackerDog generates Proxies by inheriting from our classes. Therefore, it requires that all methods and properties we want to track be declared as virtual. For example, suppose we want to track the following POCO:

public class Employee   
{
  public virtual int Id { get; set; }
  public virtual string Department { get; set; }
  public virtual string Name { get; set; }
  public virtual double Value { get; set; }
  public virtual DateTime Date { get; set; } //(datetime, null)
  public virtual Guid RecordGuid { get; set; } //(uniqueidentifier, null)
}
Copied!

First, we need to tell TrackerDog which classes we want to track, by setting up the library configuration.

var config = ObjectChangeTracking.CreateConfiguration();
config.TrackThisType<Employee>();

TrackableObjectFactory = config.CreateTrackableObjectFactory();
Copied!

Next, we can create a Proxy of our POCO:

var employee = new Employee();
employee = trackableObjectFactory.CreateFrom(employee);
Copied!

Alternatively, if our class has a parameterless constructor, we can create the Proxy directly without having to pass an instance.

var employee = trackableObjectFactory.CreateOf<Employee>();
Copied!

With our new Proxy, we can either access its properties normally, get the original value before the change, or determine if a property has changed its value.

var currentName = employee.Name;
var oldName = employee.OldPropertyValue(e => e.Name);
var hasChangeName = employee.PropertyHasChanged(e => e.Name)
Copied!

We can also get a list of all modified / unmodified properties in the instance.

var changeTracker = employee.GetChangeTracker();
var changedProperties = changeTracker.ChangedProperties;
var unchangedProperties = changeTracker.UnchangedProperties;
Copied!

On the other hand, we can undo the changes made to the instance.

employee.UndoChanges();
Copied!

Finally, if at any point we want to obtain an object of the original class, for example to serialize it, we can do:

employee = employee.ToUntracked();
Copied!

TrackerDog has many more interesting features, such as tracking changes in inherited classes, interfaces, or collections. You can find much more information on the project page, which is worth visiting in any case.

In conclusion, TrackerDog is an interesting library to solve a common problem, and it can be useful in many situations, saving us a lot of unnecessary code.