In this post I am talking about auditing for the business, not the technical infrastructure. As an example story, users will want to see certain fields highlighted on a grid if that field has changed in the last week, and the info about the change (who, when, what) should be a tooltip for the cell.
Database trigger
NH Interceptor (per NH in action and the NH docs)
NH Listener (per the Hibernate docs)
Domain model
Because auditing is a simple application of interceptors there is a lot of guidance online about that approach, and using an interceptor was my first attempt at auditing.
But since the concept of an audit entry is important to the application (and the users!), it makes sense to promote this concept to a domain object. Reporting is simple and the behavior is easier to manage. Create a class called AuditEntry (with supporting mapping file) and hang collections of AuditEntry from the entity. Manage this collection in regular behavior:
public virtual void ChangeStatus(Employee employee, DateTime time, OrderStatus status) { if(this.Status != status) { AuditEntry entry = new AuditEntry(employee, time, this.Status, status); _auditEntries.Add(entry); } Status = status; }
After having gone down the database trigger path and the interceptor path and encountering friction I now recommend the domain model approach.
3 Responses
Dew Drop - July 12, 2008 | Alvin Ashcraft's Morning Dew
12|Jul|2008 1[...] Audit Options with NHibernate and Refactoring DRY Tests with ReSharper's Inline Method (Matt Hinze) [...]
So you want to learn NHibernate? - Part 1 of 1, The Links « HSI Developer Blog
31|Jul|2008 2[...] Audit Options with NHIntegrating Castle Windsor and NHibernate with WCF - Throwing the WCF facility and some Rhino Tools in the mixBook review: NHibernate in ActionUsing Entity-Assigned ID's Possibly related posts: (automatically generated)Milla Jovovich reveals what body part she'd change… Posted in Uncategorized. [...]
So you want to learn NHibernate? (or, NHibernate Hyperlink Acupuncture) | The Freak Parade
08|Aug|2008 3[...] Audit Options with NH Integrating Castle Windsor and NHibernate with WCF - Throwing the WCF facility and some Rhino Tools in the mix Book review: NHibernate in Action Using Entity-Assigned ID's Share: These icons link to social bookmarking sites where readers can share and discover new web pages. [...]
Leave a reply