This post details the process to create a domain auditing system using NHibernate events. Upon any change to a mapped entity, details of the change are posted to an audit table in the database. Setup Create the AuditEventListener , which inherits from IPostInsertEventListener , IPostUpdateEventListener and IPostDeleteEventListener; this will enable the logging of inserts, updates and deletes respectively A custom time provider is used here, to append the current date and time to the logging, though using the native DateTime.Now is normally sufficient Add the OnPostUpdate method and pass in a PostUpdateEvent Using the entity property on the PostUpdateEvent , check to ensure the entity is not an AuditLogEntry , as it makes no sense to audit the audit entity Verify that the old state of the entity is not null. If the old state is null, it is clear that this is not an update, and so make the method throw an error if this is the case U...
My name is Ben and I am a software developer from Sussex, England. The purpose of this blog is to keep a record of technologies I utilise throughout my career as a software developer. I believe that not only will this further cement my understanding of these technologies, but also serve as a useful personal resource for future work.