Like many other places, we have some tables in our database with audit fields such as LastModifiedTimestamp and LastModifiedBy. However, not all tables have these fields and some tables might only have one of them. (Yes, this is a database consistency issue first, but the database is more than 20 years old and it's not getting fixed this year. )
I'd like to manage this from one piece of code. Essentially, before saving, test for IsDirty/IsNew and update the timestamp and/or user id if the entity has changed or is new.
There are two previous threads that touch on this topic.
https://llblgen.com/tinyforum/Messages.aspx?ThreadID=3612
https://llblgen.com/tinyforum/Messages.aspx?ThreadID=2921
I could override DataAccessAdapter but I'd need to do some kind of test for whether either of those fields were in the entity being saved.
(From this thread https://llblgen.com/tinyforum/Messages.aspx?ThreadID=3612)
/// <summary>
/// Overrides OnBeforeEntitySave. This allows us to "interrupt" the save of an entity to populate audit fields.
/// </summary>
/// <param name="entitySaved"></param>
/// <param name="insertAction"></param>
public override void OnBeforeEntitySave(IEntity2 entitySaved, bool insertAction)
{
string auditSaveType = string.Empty;
if (entitySaved.IsNew)
{
//need to test whether this entity actually has these audit fields
entitySaved.SetNewFieldValue("CreatedByUserId", _userId);
entitySaved.SetNewFieldValue("CreatedDate", DateTime.Now);
}
if (entitySaved.IsDirty)
{
//need to test whether this entity actually has these audit fields
entitySaved.SetNewFieldValue("LastUpdatedByUserId", _userId);
entitySaved.SetNewFieldValue("LastUpdatedDate", DateTime.Now);
}
base.OnBeforeEntitySave(entitySaved, insertAction);
}
I'm looking for any suggestions on how to handle this. Is overriding the DataAccessAdapter the best approach from an application perspective? What would be the most efficient way to test for field existence? Or is there a better way (something like AOP perhaps)?