This is the beginning of a multi-part post series on writing simple auditing functionality for an ASP.NET application using NHibernate. The requirement was that every object modification event in the system should be logged by username and date. Specifically I don’t need to know exactly which properties were changed (just that a user was updated by whom at what time), but if you do need to save the changed properties there are plenty of hooks to do that.
I’ll update this post with links to the next parts when they become available
The Audit Class
The Audit class will inherit from a base DomainObject<> which basically provides an Id property for all objects and helper methods for validation and transient checks (this is pretty common architecture when using NHibernate). I’m using EntLib validation on the class and to satisfy a particular requirement I am going to set the action type to C/U/D depending on the action. Here’s the code:
Audit Repository Testing
Before I move on to creating the interceptor I am going to run a few tests against a SQLite repository to make sure the Audit class can save and validate properly.
And to test that saving works properly:
What Did This Accomplish?
Not much yet. We have an audit class that is behaving how we want, and eventually we are going to use it to save audit records. Next time we’ll create an IInterceptor which will hook into create/update/delete events in NHibernate.