我在域模型中为ILoggable对象设置了一个IInterceptor。在OnFlushDirty事件中,我正在尝试保存日志(审计)。但是在这样做时,我的代码进入了一个无限循环。
_logrepository.Save(log)调用OnFlushDirty,即使log不是ILoggable对象(因为实体仍然是前一个对象)
有没有办法在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);
}
答案 0 :(得分:0)