IPreUpdateEventListener和dynamic-update ="true". [英] IPreUpdateEventListener and dynamic-update="true"
问题描述
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屋!