In my application, I have several different types of entities that I have a requirement for tracking changes at the field level. I searched for it for Google and could only come up with examples of performing simple checks of “Date Inserted” and “Date Update” when you simply change the fields of an existing object. My requirements are more complex because:
- I need to create new entities that represent changes that do not just change the fields for the entity being saved (as is the case with the dates you found, examples Update date).
- I need to save these new objects in the database in the same transaction that updates the tracked object
- I need to track changes in a ValueObjects collection attached to a tracked object
- I do not want to write a separate registration code for each tracked object
Code example:
public interface ITrackChanges {}
{
}
public class Account : ITrackChanges
{
public int Id;
public string AccountNumber;
public string CustomerName;
public string CustomerAddress;
}
public class Computer : ITrackChanges
{
public int Id;
public string AssetTag;
public string SerialNumber;
public IEnumerable<IPAddress> IPAddresses;
}
public class IPAddress : ValueObject
{
public string Address;
}
Whenever any of the values of the Account or Computer object changes or the list of IP addresses associated with the Computer object changes, I need to create and save these entity change records:
public class EntityChange
{
public string EntityType;
public string EntityId;
public string PropertyName;
public string OldValue;
public string NewValue;
}
Each monitored object implements the ITrackChanges marker interface, and each value object is inherited from the ValueObject base class. Value objects are displayed as components in NHibernate.
, , Computer Id 1 AssetTag "ABC123" "ABC124" IP- { 1.2.3.4 "} {" 1.2.3.4 "," 1.2.3.5 "}, 2 EntityChange:
EntityChange
{
EntityType = "Computer"
EntityId = 1
PropertyName = "AssetTag"
OldValue = "ABC123"
NewValue = "ABC124"
}
EntityChange
{
EntityType = "Computer"
EntityId = 1
PropertyName = "IPAddresses"
OldValue = "1.2.3.4"
NewValue = "1.2.3.4, 1.2.3.5"
}
? , , / , - , . .
, , -, NHibernate? , LLBLGen ORM, , NHibernate.