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, ie the developer should not have to do any extra work, ie 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:
Has anyone ever worked on this problem or know of a good approach to solve it?
I've done some similar work on auditing changes -- which is the first step. My solution works by making my designer context abstract then deriving the real context from it and overriding SubmitChanges. It works in conjunction with a separate audit context and a helper class that can take an object and construct the audit object from it. It relies on attributes to give the necessary information on which class is the audit class for a particular object.
You can find more information on my blog at http://farm-fresh-code.blogspot.com . It's too involved to repeat here.
For your first question, you can get access to the objects awaiting changes with db.GetChangeSet()
and to know which fields changed and what was the original value, you can use:
ITable table = db.GetTable(entity.GetType());
ModifiedMemberInfo[] modifiedMembers = table.GetModifiedMembers(entity);
object original = table.GetOriginalEntityState(entity);
Since L2S generates code for you, and you wish to alter the behaviour of the generated code I see two possibilities:
I hope these pointers can help you on your way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.