문제

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? 
}
도움이 되었습니까?

해결책

NHibernate Envers helps you with that.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top