我在我的域模型中为 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);
}