IPreUpdateEventListener 和 dynamic-update="true" [英] IPreUpdateEventListener and dynamic-update="true"
问题描述
我一直在尝试按照 Ayende 的博客,这似乎是每个人在谈到 IPreUpdateEventListener 和 IPreInsertEventListener 时都会参考的资源.
I have been trying to do a very simple auditing scenario following Ayende's blog which seem to be the resource everyone is refering to when it comes to IPreUpdateEventListener and IPreInsertEventListener.
然而,无论我多么努力,我都无法让它发挥作用.事件正确触发,当我通过它时一切看起来都很好,但是我的changedtime"没有更新到数据库.
However no matter how hard I tried, I couldn't get it to work. The event fired correctly, everything looked ok when I stepped through it but no update of my "changedtime" was ever issued to the database.
我在谷歌上搜索了大约一天,终于找到了答案这里.
I spent about a day googling this and finally found the answer here.
当您将实体映射为 dynamic-update="true" 时,它就无法工作.果然,我就是这种情况.既然这么难找这个资料,用dynamic-update="true"是不是不常见?我们在所有实体上都使用它.
It just won't work when you have your entity mapped with dynamic-update="true". And sure enough, that was the case for me. Since it was so hard for me to find this piece of information, is it uncommon to use dynamic-update="true"? We use it on all our entities.
因为这对我们来说是一个重大的障碍,所以我想问一下有没有办法解决这个问题?
As this is a major bump in the road for us I wanted to ask if there's any way around this at all?
我一直在研究 IInterceptor,但它总是被称为过时的,那么它的缺点是什么?此外,我还没有找到关于如何使用 IInterceptor 归档相同审计(带有插入/更新时间戳)的非常好的教程(我对 NHibernate 还很陌生).
I have been looking at IInterceptor but it's always refered to as outdated, so what's the drawbacks with this? Also I haven't been able to find a really good tutorial on how to archieve the same auditing (with insert/update timestamps) with IInterceptor (I'm fairly new to NHibernate).
任何帮助将不胜感激!
推荐答案
我遇到了这个确切的问题.这是我修复它的方式:
I ran into this exact issue. This is how I fixed it:
public class MyFlushEntityEventListener : DefaultFlushEntityEventListener
{
protected override void DirtyCheck(FlushEntityEvent e)
{
base.DirtyCheck(e);
if (e.DirtyProperties != null &&
e.DirtyProperties.Any() &&
//ITrackUpdate is my inteface for audited entities
e.Entity is ITrackUpdate)
e.DirtyProperties = e.DirtyProperties
.Concat(GetAdditionalDirtyProperties(e)).ToArray();
}
static IEnumerable<int> GetAdditionalDirtyProperties(FlushEntityEvent @event)
{
yield return Array.IndexOf(@event.EntityEntry.Persister.PropertyNames,
"UpdateTime");
yield return Array.IndexOf(@event.EntityEntry.Persister.PropertyNames,
"UpdateUser");
//You can add any additional properties here.
//Some of my entities do not track the user, for example.
}
}
然后,只需替换 NH 配置文件中的事件侦听器:
Then, just replace the event listener in the NH config file:
<listener type="flush-entity"
class="MyFlushEntityEventListener, MyAssembly"/>
这篇关于IPreUpdateEventListener 和 dynamic-update="true"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!