Data Auditing in NHibernate and SqlServer

前端 未结 6 1550
误落风尘
误落风尘 2020-12-02 19:30

I\'m using NHibernate on a project and I need to do data auditing. I found this article on codeproject which discusses the IInterceptor interface.

What is your pref

6条回答
  •  执笔经年
    2020-12-02 20:04

    As an entirely different approach, you could use the decorator pattern with your repositories.

    Say I have

    public interface IRepository where EntityType:IAuditably
    { 
        public void Save(EntityType entity);
    }
    

    Then, we'd have our NHibernateRepository:

    public class NHibernateRepository:IRepository
    {
       /*...*/
       public void Save ( EntityType entity )
       {
           session.SaveOrUpdate(entity);
       }
    }
    

    Then we could have an Auditing Repository:

    public class AuditingRepository:IRepository
    {
       /*...*/
       public void Save ( EntityType entity )
       {
           entity.LastUser = security.CurrentUser;
           entity.LastUpdate = DateTime.UtcNow;
           innerRepository.Save(entity)
       }
    }
    

    Then, using an IoC Framework (StructureMap, Castle Windsor, NInject) you could build it all up without the rest of your code every knowing you had auditing going on.

    Of course, how you audit the elements of cascaded collections is another issue entirely...

提交回复
热议问题