在 NHibernate IInterceptor 中保存对象
我在域模型中为 ILoggable 对象设置了一个 IInterceptor。在 OnFlushDirty 事件中,我试图保存日志(审核)。但是在执行此操作时,我的代码进入无限循环。
即使 log 不是 ILoggable 对象(这是因为实体仍然是前一个对象),_logrepository.Save(log) 也会调用 OnFlushDirty
有没有办法在 IInterceptor 中使用 .Save (无需打开另一个会话),或者如何插入在 IInterceptor 中登录数据库?
public override bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)
{
//find diffs, log each change
if (entity is ILoggable && entity is NamedEntity)
{
var namedEntity = entity as NamedEntity;
for (var i = 0; i < currentState.Count(); i++)
{
if (currentState[i] != previousState[i])
{
var log = new Log()
{
Description = "update",
InDate = namedEntity.ModifiedDate.Value,
ItemId = namedEntity.Id,
ItemType = namedEntity.GetType().Name,
NewValue = currentState[i].ToString(),
OldValue = previousState[i].ToString(),
PropertyName = propertyNames[i],
UserId = namedEntity.ModifiedByUserId.Value
};
// calling save calls onflushdirty again, where entity is not log, but the same entity of the first call
ObjectFactory.GetInstance().Save(log);
}
}
}
return base.OnFlushDirty(entity, id, currentState, previousState, propertyNames, types);
}
I set up an IInterceptor for ILoggable objects in my domain model. And on OnFlushDirty event, i am trying to save a log (audit). But while doing this, my code goes into an infinite loop.
_logrepository.Save(log) calls OnFlushDirty even if log is not an ILoggable object (it is because entity is still the previous object)
Is there a way to use .Save (without opening another session) in IInterceptor, or how do I insert a log into database in IInterceptor?
public override bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)
{
//find diffs, log each change
if (entity is ILoggable && entity is NamedEntity)
{
var namedEntity = entity as NamedEntity;
for (var i = 0; i < currentState.Count(); i++)
{
if (currentState[i] != previousState[i])
{
var log = new Log()
{
Description = "update",
InDate = namedEntity.ModifiedDate.Value,
ItemId = namedEntity.Id,
ItemType = namedEntity.GetType().Name,
NewValue = currentState[i].ToString(),
OldValue = previousState[i].ToString(),
PropertyName = propertyNames[i],
UserId = namedEntity.ModifiedByUserId.Value
};
// calling save calls onflushdirty again, where entity is not log, but the same entity of the first call
ObjectFactory.GetInstance().Save(log);
}
}
}
return base.OnFlushDirty(entity, id, currentState, previousState, propertyNames, types);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我明白了,我的问题是 _logrepository.Save(log) 正在打开另一个事务并提交它。
因此,我更改了 logrepository.Save(log) 不打开并提交另一个事务,而是使用打开的事务。
ok, i figured it out, my problem was _logrepository.Save(log) was opening ANOTHER transaction and commiting it.
So, i changed logrepository.Save(log) not to open and commit another transaction but use an open transaction.