Question

We have a web app where we use NHibernate as a ORM, and now we need to add auditing on some of the entities to track who changed what. However, I have not managed to figure out the best and easiest way to handle it.

If possible I would like the audit info to go into a separate table for each entity, something like this (pseudo sql):

create table MethodStatus (MethodId, Enabled)
create table MethodStatusAudit (MethodId, Enabled, AdminId, Date, ChangeType(U,D,I))

The question is how to setup this with (Fluent)NHibernate so that its somewhat easy to manage?

My initial idea was to use a IPreInsertEventListener / IPreUpdateEventListener / IPreDeleteEventListener and have my auditable objects implement a IAuditable interface and then do something. But I cant figure out how to save the audit..

public interface IAuditable {}

public class MethodStatus : IAuditable
{
    public virtual int MethodId { get; set; }
    public virtual bool Enabled { get; set; }
}

public class MethodStatusMap : ClassMap<MethodStatus>
{
    public MethodStatusMap()
    {
        Id(x => x.MethodId);
        Map(x => x.Enabled);
    }
}

public bool OnPreInsert(PreInsertEvent @event)
{
    var e = @event.Entity as IAuditable;
    if (e != null)
        //save audit.. but how? 
}
Was it helpful?

Solution

NHibernate Envers helps you with that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top