我为域模型中的ILoggable对象设置了一个IInterceptor。对于OnFlushDirty事件,我正在尝试保存日志(审计)。但是在这样做的时候,我的代码进入了无限循环。
_logrepository.Save( log )调用OnFlushDirty,即使log不是ILoggable对象(因为entity仍然是前一个对象)
有没有办法在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);
}发布于 2009-09-29 09:03:56
好吧,我弄明白了,我的问题是_logrepository.Save(日志)正在打开另一个事务并提交它。因此,我更改了logrepository.Save(日志),不再打开和提交另一个事务,而是使用一个打开的事务。
https://stackoverflow.com/questions/1491154
复制相似问题