Language: EN

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

How to track changes in C# entities with TrackerDog

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

When we are handling an ORM like Entity Framework or NHibernate, the ORM itself has change tracking, so that it can update in the DB only the values that have really changed.

However, if we use a micro ORM like Dapper, or even without using a database at all, we frequently find situations where it is necessary to register the changes of a series of objects.

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

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

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

Install TrackerDog

TrackerDog is available through 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

Using TrackerDog

As we have said, TrackerDog generates Proxys by inheritance of ours. Therefore, it requires that all the methods and properties that we want to register 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)
}

First, we have to tell TrackerDog the classes we want to register, establishing the configuration of the library.

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

TrackableObjectFactory = config.CreateTrackableObjectFactory();

Next, we can create a Proxy of our POCO:

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

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

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

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

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

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

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

employee.UndoChanges();

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

employee = employee.ToUntracked();

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

In conclusion, TrackerDog is an interesting library to solve a common problem, and that can be useful to us in many situations avoiding a large amount of unnecessary code.