基类中的审计信息

时间:2012-03-28 19:49:14

标签: c# asp.net asp.net-mvc visual-studio nhibernate

我正在使用NHIBERNATE创建一个ASP.NET MVC3应用程序。我有一个基类实体,用于捕获一些审计信息,如CreatedBy,CreatedOn,UpdatedBy,UpdatedOn等。每当创建和保持/更新派生类时,这些属性应自动填充。< / p>

由于实体类型以及所有其他类型都在 DOMAIN 程序集中定义,从中获取 USER 信息的最佳方式是什么网络投射到 DOMAIN 以填充实体基类中的信息

修改

此外,

  1. 目前,我将所有属性定义为 public virtual ,因此我如何实现它们,以便在持久性时动态创建dates / userId。
  2. 我希望Base类属性在每个表中持久存在于派生的具体类(每个具体类的表)。如何使用FLUENT NHIBERNATE指定它(我不使用自动映射)

1 个答案:

答案 0 :(得分:2)

打开会话时使用拦截器:

sessionFactory.OpenSession(new AuditInterceptor());

文件:AuditInterceptor.cs

using System;
using NHibernate;
using NHibernate.Type;
using System.Threading;
using System.Security;

namespace bla
{
    /// <summary>
    /// NHibernate Interceptor that stores audit information (idlogin and datetime.now) on every save and update
    /// </summary>
    public class AuditInterceptor : EmptyInterceptor
    {

        private int updates;
        private int creates;
        private int loads;
        private ISession session;

        public override void OnDelete(object entity,
                                      object id,
                                      object[] state,
                                      string[] propertyNames,
                                      IType[] types)
        {
            // do nothing
        }

        public override bool OnFlushDirty(object obj, object id, object[] currentState, object[] previousState, string[] propertyNames, IType[] types)
        {
            bool found = false;
            if (obj is Entity)
            {
                updates++;
                for (int i = 0; i < propertyNames.Length; i++)
                {
                    if ("dtlastchanged".Equals(propertyNames[i].ToLower()))
                    {
                        currentState[i] = DateTime.Now;
                        found = true;
                    }
                    if ("lastchangedby".Equals(propertyNames[i].ToLower()))
                    {
                        currentState[i] = this.session.Get<Login>(IdLogin, LockMode.None);
                        found = true;
                    }
                }
            }
            return found;
        }

        public override bool OnLoad(object obj, object id, object[] state, string[] propertyNames, IType[] types)
        {
            if (obj is Entity)
            {
                loads++;
            }
            return false;
        }

        public override bool OnSave(object entity,
                                    object id,
        object[] state,
        string[] propertyNames,
        IType[] types)
        {
            bool found = false;
            if (entity is Entity)
            {
                creates++;
                for (int i = 0; i < propertyNames.Length; i++)
                {
                    if ("dtlastchanged".Equals(propertyNames[i].ToLower()))
                    {
                        state[i] = DateTime.Now;
                        found = true;
                    }
                    if ("lastchangedby".Equals(propertyNames[i].ToLower()) && !(entity is Login && (entity as Login).IsInVerification))
                    {
                        state[i] = this.session.Get<Login>(IdLogin, LockMode.None);
                        found = true;
                    }
                }
            }
            return found;
        }

        public override void AfterTransactionCompletion(ITransaction tx)
        {
            //if (tx.WasCommitted)
            //{
            //    System.Console.WriteLine("Creations: " + creates + ", Updates: " + updates, "Loads: " + loads);
            //}
            updates = 0;
            creates = 0;
            loads = 0;
        }

        public override void SetSession(ISession session)
        {
            this.session = session;
            base.SetSession(session);
        }

        protected long IdLogin
        {
            get
            {
                return (Thread.CurrentPrincipal.Identity as CustomIdentity).IdLogin; // or something else that holds the id of the current logged in user
            }
        }
    }
}