Auditable Entities

Jan 2, 2015 at 4:43 AM
Edited Jan 2, 2015 at 4:44 AM
I have a some entities that implement an IAuditable interface:
public interface IAuditable
    string CreatedBy { get; set; }
    DateTime Created { get; set; }
    string UpdatedBy { get; set; }
    DateTime? Updated { get; set; }
What is the best practise using your framework to ensure that these field are consistently updated when these entities are persisted?

Thank you.
Jan 2, 2015 at 11:11 AM
Edited Jan 2, 2015 at 11:16 AM
We've done this a few times for other teams/projects, simply add your logic to SyncObjectsStatePreCommit() in DataContext.cs, this method iterates through all entities before SaveChanges occur. During iteration, cast the entities into check to see if it implement IAuditable (performing a soft cast and checking for null) and update our fields accordingly.
Marked as answer by lelong37 on 1/2/2015 at 3:12 AM
Jan 2, 2015 at 1:19 PM
Thank you the solution is clear, but is it possible to do this in a way that will make it unnecessary to change your code?
Jan 2, 2015 at 1:27 PM
Edited Jan 2, 2015 at 1:28 PM
Yes, we are adding in domain (driven) events which you could simply add an event to do this which wouldn't need any changes to the framework code, however this won't be until the next release.

For now don't be intimidated to add your code into the framework, the framework was deliberately designed to be lightweight (as of now only 10 classes including the EF provider) so that it's non-intimidating to extend and customize. When the next release is available you can simply copy and paste the same code into the subscribed event for SyncObjectsStatePreCommit.
Jan 2, 2015 at 1:29 PM
Excellent, much appreciated.