简体   繁体   English

NHibernate拦截器不起作用

[英]NHibernate Interceptor not working

I have set up an IIntercpetor to update the timestamp (last modification date) of entities when they are saved or updated. 我设置了一个IIntercpetor来在实体保存或更新时更新它们的时间戳(最后修改日期)。

My implementaion looks like this: 我的实现看起来像这样:

public class NhInterceptor : EmptyInterceptor
{
    public override bool OnSave(object entity, object id, object[] state, 
        string[] propertyNames, NHibernate.Type.IType[] types)
    {
        var baseEntity = entity as EntityBase;
        if (baseEntity == null)
            return false;

        baseEntity.LastModification = DateTime.Now;
        return true;
    }

    public override bool OnFlushDirty(object entity, object id, 
        object[] currentState, object[] previousState, 
        string[] propertyNames, NHibernate.Type.IType[] types)
    {
        var baseEntity = entity as EntityBase;
        if (baseEntity == null)
            return false;

        baseEntity.LastModification = DateTime.Now;
        return true;
    }
}

This code is hit, and debugger shows that baseEntity.LastModification has the right value, just before returning. 这段代码被点击,调试器显示baseEntity.LastModification在返回之前具有正确的值。

However, my Json output (in Web API) shows LastModification as 0001-01-01T00:00:00 and if i check my created entity in database, it shows same result as well. 但是,我的Json输出(在Web API中)显示LastModification0001-01-01T00:00:00 ,如果我检查数据库中创建的实体,它也会显示相同的结果。

Why does this not work? 为什么这不起作用?

You need to change the currentState values. 您需要更改currentState值。 First lookup in propertyNames for "LastModification", then change currentState of the index if found. 首先在propertyNames中查找“ LastModification”,然后更改索引的currentState(如果找到)。 You can check in IType[] for the type as well. 您也可以在IType []中检查类型。

I decided to give you my working solution, based on Hylaean 's answer. 我决定根据Hylaean的回答为您提供我的工作解决方案。

My Interceptor class: 我的拦截器课程:

public class NhInterceptor : EmptyInterceptor
{
    public override bool OnSave(object entity, object id, object[] state, 
        string[] propertyNames, NHibernate.Type.IType[] types)
    {
        var baseEntity = entity as EntityBase;
        if (baseEntity == null)
            return false;

        var lastModificationPropName = ExpressionUtil
            .GetPropertyName<EntityBase>((e) => e.LastModification);

        for (int i = 0; i < propertyNames.Length; i++)
        {
            if (lastModificationPropName == propertyNames[i])
            {
                state[i] = DateTime.Now;
                return true;
            }
        }

        return true;
    }

    public override bool OnFlushDirty(object entity, object id, 
        object[] currentState, object[] previousState, 
        string[] propertyNames, NHibernate.Type.IType[] types)
    {
        var baseEntity = entity as EntityBase;
        if (baseEntity == null)
            return false;

        var lastModificationPropName = ExpressionUtil
            .GetPropertyName<EntityBase>((e) => e.LastModification);

        for (int i = 0; i < propertyNames.Length; i++)
        {
            if (lastModificationPropName == propertyNames[i])
            {
                currentState[i] = DateTime.Now;
                return true;
            }
        }
        return true;
    }
}

My Expression util class: 我的Expression util类:

public static class ExpressionUtil
{
 public static string GetPropertyName<T>(Expression<Func<T, object>> expression)
    {
        var body = expression.Body as MemberExpression;

        if (body == null)
        {
            body = ((UnaryExpression)expression.Body).Operand as MemberExpression;
        }

        return body.Member.Name;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM