在 NHibernate IInterceptor 中保存对象

发布于 2024-08-05 21:45:22 字数 1628 浏览 2 评论 0原文

我在域模型中为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝咒 2024-08-12 21:45:22

好吧,我明白了,我的问题是 _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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文