Background:
I have a web application for which I need to do database auditing for insert/deletes/updates (and maybe reads). I use LINQ as my ORM. Using some ideas I found on the web I've come up with a way of using attributes to decorate my entities which have associated audit tables. The audit table itself is required to include the same columns with the same types as the original table in addition to fields for the id and name of the current user, modification type, modification time, and whether the operation was successful or not. The auditing occurs during SubmitChanges -- my data context is abstract and I inherit and override SubmitChanges in my concrete implementation. The abstract data context actually derives from an AbstractAuditableDataContext which extends DataContext and adds a CurrentUser property with placeholders for the current user id and name. By default these are 0 and "system" for the instances where there isn't a logged in user -- say during registration or login when certain fields of the user table may be updated. The application is written in C# using ASP.NET MVC.
Problem:
What's the best way to populate the current user property of my derived data context? Should I create a utility class that gets injected in the AuditUtility that checks to see if the CurrentUser has been set and, if not, fills it in. For testing I'd mock this out, but in the live application it would probably use lazy-loading and get/set it in the session. Or should I modify the data context factory (used by all controllers) to perform this functionality. I already use a mock factory during unit testing so this wouldn't involve creating new classes. Or should the derivation be done outside the factory and the current user injected in during context creation. This would allow me to do "on behalf of" auditing.
I realize that this is somewhat subjective, but I'd appreciate any thoughts/experiences you might contribute.
Thanks.