简体   繁体   English

NHibernate拦截器-OnFlushDirty

[英]NHibernate Interceptor - OnFlushDirty

What I want to do is to intercept entity on save and truncate some string properties. 我想要做的是在保存时拦截实体并截断一些字符串属性。

The issues is that OnSave() triggers only for new Entity , but not on changing/updating Entity . 问题在于OnSave()仅针对新Entity触发,而不针对更改/更新Entity触发。

Then there is OnFlushDirty() interceptor. 然后是OnFlushDirty()拦截器。 But I am really confused with this one. 但是我真的对此感到困惑。

So I want to modify some property of entity on updating entity, like: 所以我想在更新实体时修改实体的某些属性,例如:

public bool OnFlushDirty( 
             object entity, 
             object id, 
             object[] currentState, 
             object[] previousState,  
             string[] propertyNames, 
             IType[] types)
{
      bool entityChanged = false;
      for (int i = 0; i < propertyNames.Length; i++)
      {
            var stringType = types[i] as NHibernate.Type.StringType;

            if (stringType != null)
            {
                 if(NeedTruncate(currentState[i]))
                 {
                      currentState[i] = Truncate(currentState[i]);
                      entityChanged = true;
                 }
            }
      }

      return entityChanged;
}

So the issue is next. 因此,下一个问题是。 On some blogs like this one I read that OnFlushDirty() is called when entity was changed and saved into database, so the changes to any property here will not be saved into database. 在类似这样的一些博客上,我读到OnFlushDirty()是在实体更改并保存到数据库时被调用的,因此对此处任何属性的更改都不会保存到数据库中。

But in githab.io documentation it says: 但是在githab.io文档中它说:

The interceptor may modify the detected currentState, which will be propagated to both the database and the persistent object. 拦截器可以修改检测到的currentState,将其传播到数据库和持久对象。 Note that all flushes end in an actual synchronization with the database, in which as the new currentState will be propagated to the object, but not necessarily (immediately) to the database. 请注意,所有刷新都以与数据库的实际同步结束,在此同步中,新的currentState将传播到对象,但不一定(立即)传播到数据库。 It is strongly recommended that the interceptor not modify the previousState. 强烈建议拦截器不要修改previousState。

It also says: 它还说:

Called when an object is detected to be dirty, during a flush. 在刷新期间检测到对象变脏时调用。

So I am not sure what exactly means 所以我不确定到底是什么意思

Called when an object is detected to be dirty 当检测到对象变脏时调用

Also regarding to this documentation, the changes will be saved into database, just not necessary immediately. 另外,关于本文档,所做的更改将保存到数据库中,只是不需要立即进行。

Anyway, by testing this in my project, this works and when OnFlushDirty() is called and it changes value of some property it's also saved in database and all seams to be working fine. 无论如何,通过在我的项目中对此进行测试,它可以正常工作,并且在OnFlushDirty()并更改某些属性的值时,该属性也保存在数据库中,并且所有接缝OnFlushDirty()正常工作。

So my question is: Is this correct to do? 所以我的问题是:这样做正确吗? As I said, I tested it with my project and on changing/updating entity OnFlushDirty() is called, property is changed and saved into database as well. 正如我所说,我在项目中对其进行了测试,并在更改/更新实体OnFlushDirty()进行了调用,属性也被更改并保存到数据库中。 Just because all of this, and weak documentation, I am not sure if I can run into some issues with this? 仅仅因为所有这些和文档薄弱,我不确定是否会遇到一些问题?

What I do, in a similar case, is override the FindDirty method of the interceptor, and return an array of indexes for the properties that have changes (or that you will change in the flushdirty). 在类似情况下,我要做的是重写拦截器的FindDirty方法,并为具有更改(或将在flushdirty中更改)的属性返回索引数组。

In your case, you could override that method and check in that method whether or not you have some string properties that should be truncated. 在您的情况下,您可以覆盖该方法并检查该方法是否具有一些应被截断的字符串属性。 If so, return the indexes of those properties. 如果是这样,则返回这些属性的索引。

By doing so, you should be sure that OnFlushDirty is always called when it is necessary for you. 这样,您应该确保在需要时始终调用OnFlushDirty

Next to that, OnFlushDirty should also return true if you have modified a persistent property in that method, which you do not do as far as I can see in your code that is posted here. 紧接着,如果您在该方法中修改了持久属性,则OnFlushDirty也应返回true,就我在此处发布的代码中所看到的而言,您不会这样做。

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

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