I keep track of all changes that are made to objects so that the user can view and rollback to any previous version of any item in the database.
The history database table looks like this:
Item | ItemId | Field | WhenChanged | OldValue | NewValue
customer | 6 | LastName | 2009-12-31 13:00:04 | Sanders | Sanders-Smith
customer | 5 | FirstName | 2009-12-31 12:11:14 | Jym | Jim
Currently I record these changes whenever the user fills out a form so I have complete information of the old and new state of the object.
However, now I need to make this historical data logging available from code. It needs to work transparently when LINQ-to-SQL is used, i.e. the developer should not have to do any extra work, i.e. the following code should cause a write to the history table as well:
using (var db = Datasource.GetContext())
{
var customers = from c in db.Customers
where c.Status == "waiting"
select c;
foreach(var customer in customers)
{
customer.Status = "finished";
}
}
db.SubmitChanges();
I can imagine I can accomplish this in two ways:
- override db.SubmitChanges() but then the question is how do I get access to the objects which are awaiting changes.
- attach my logging method to an OnSubmitChanges event but I haven't been able to find a solution to this yet
Has anyone ever worked on this problem or know of a good approach to solve it?