patterncsharpMinor
Tracking which entity properties have changed
Viewed 0 times
trackingpropertieswhichchangedhaveentity
Problem
There are a number of topics pertaining to entity change tracking. All the ones I've seen involve either 1) notifying when a property has changed or 2) merely determining whether or not an entity is dirty.
My goal is quite different. I wrote a mini-repository framework that generates SQL for inserts, selects, updates, and deletes. Updates are particularly tricky because the framework needs to support partial updates. To do this, you have to know which properties have changed and only generate the update SQL for those specific fields. Otherwise, you run the risk of overwriting existing data with nulls or default values unless you load the original entity from the database prior to the update.
Take this person class as an example:
Quite a bit of boilerplate code would be required to allow it to track its own property changes. Here's an example (leaving LastName and DateOfBirth alone for brevity):
Imagine having to do this for 50 properties or more (some of the classes I work with have 90+ properties).
So I came up with a special tracking class that uses generics and expressions to encapsulate all of that boilerplate property code. Having actually begun to use it in my application, I've developed a bit of a love/hate relationship with it.
Here's the watered-down version o
My goal is quite different. I wrote a mini-repository framework that generates SQL for inserts, selects, updates, and deletes. Updates are particularly tricky because the framework needs to support partial updates. To do this, you have to know which properties have changed and only generate the update SQL for those specific fields. Otherwise, you run the risk of overwriting existing data with nulls or default values unless you load the original entity from the database prior to the update.
Take this person class as an example:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
}Quite a bit of boilerplate code would be required to allow it to track its own property changes. Here's an example (leaving LastName and DateOfBirth alone for brevity):
public class Person
{
HashSet ChangedProperties =
new HashSet(StringComparer.OrdinalIgnoreCase);
string _FirstName = null;
public string FirstName
{
get { return _FirstName; }
set
{
ChangedProperties.Add(nameof(FirstName));
_FirstName = value;
}
}
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public string[] GetChangedProperties()
{
return ChangedProperties.ToArray();
}
}Imagine having to do this for 50 properties or more (some of the classes I work with have 90+ properties).
So I came up with a special tracking class that uses generics and expressions to encapsulate all of that boilerplate property code. Having actually begun to use it in my application, I've developed a bit of a love/hate relationship with it.
Here's the watered-down version o
Solution
I suggest you using static AOP something like Fody or Postsharp , you do not need change you entity code, no implement no change code, just add a Attribute to POCO class, TrackChange.Fody can perfect to solve your problem, https://github.com/jrt324/TrackChange
Context
StackExchange Code Review Q#128113, answer score: 2
Revisions (0)
No revisions yet.