假/真对 IPreInsertEventListeners 真正意味着什么? [英] What false/true really mean for IPreInsertEventListeners?

查看:24
本文介绍了假/真对 IPreInsertEventListeners 真正意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现了如何使用 NHibernate.Event 中的 IPreDeleteEventListenerIPreInsertEventListenerIPreUpdateEventListener 审计实例代码> 命名空间.

I recently have found out how to audit instances with the IPreDeleteEventListener, IPreInsertEventListener and IPreUpdateEventListener in the NHibernate.Event namespace.

然而,它仍然让我感到困惑,这些事件将在成功或不成功的终结中返回什么.

However, it still confuses me what shall these event return either on successful or unsuccessful finality.

例如,让我们看看 Ayende 的博客文章,该文章位于此处:

For example, let's take a look at Ayende's blog article found here:

按照他的例子,可以实现如下接口:

Following his example, one could implement the interfaces as following:

public class AuditEventListener : IPreInsertEventListener {
    public bool OnPreInsert(OnPreInsert @event) {
        var audit = @event.Entity as IHaveAuditInformation;
        if (audit == null) return false;

        var time = DateTime.Now;
        var name = WindowsIdentity.GetCurrent().Name;

        Set(@event.Persister, @event.State, "CreatedAt", time);
        Set(@event.Persister, @event.State, "CreatedBy", name);

        audit.CreatedAt = time;
        audit.CreatedBy = name;

        return false;
    }
}

返回 truefalse 作为返回值实际上意味着什么,因为我还有另一个示例,其中返回 true 而不是false 正如 Ayende 所写.

What odes it actually mean to return either true or false as return value, since I have another example where true is returned instead of false as Ayende wrote.

似乎返回 true 而不是 false.

Which seems to return true instead of false.

public class SoftDeletableListener : IPreDeleteEventListener {
    public void Register(Configuration cfg) {
        cfg.EventListeners.PreDeleteEventListeners = 
            new IPreDeleteEventListener[] { this }
                .Concat(cfg.EventListeners.PreDeleteEventListeners)
                .ToArray();
    }

    public Boolean OnPreDelete(PreDeleteEvent @event) {
        ISoftDeletable softDeletable = @event.Entity as ISoftDeletable;

        if (softDeletable == null) return true;

        EntityEntry entry = @event.Session
            .GetSessionImplementation()
            .PersistenceContext
            .GetEntry(@event.Entity);
        entry.Status = Status.Loaded;

        softDeletable.Deleted = true;

        Object id = @event.Persister
            .GetIdentifier(@event.Entity, @event.Session.EntityMode);
        Object [] fields = @event.Persister
            .GetPropertyValues(@event.Entity, @event.Session.EntityMode);
        Object version = @event.Persister
            .GetVersion(@event.Entity, @event.Session.EntityMode);

        @event.Persister.Update(id
            , fields
            , new Int32[1]
            , false
            , fields
            , version
            , @event.Entity
            , null
            , @event.Session.GetSessionImplementation());

        return true;
    }
}

所以我想知道,false/true 实际上告诉 NHibernate 取决于所处理的侦听器.

So I wonder, what false/true actually tells NHibernate depending on the listener dealt with.

推荐答案

在这种情况下返回的值应该是 enum,我们使用名称 OnPreEventResult,这些将是可能的值:

The returned value in this case should be enum, Let's use the name OnPreEventResult, and these would be the possible values:

  • OnPreEventResult.Continue => 继续当前返回 false
  • OnPreEventResult.Break => 目前,当返回 true 时,Action>中止
  • OnPreEventResult.Continue => to continue currently return false
  • OnPreEventResult.Break => at the moment, when the true is returned the Action is aborted

因此,如上面的两个示例所示,我们可以使用返回值来管理执行流程:

So, as both examples above show, we can use the return value to manage the execution flow:

  1. 继续:
    如果我们在AuditEventListener 中返回false,我们实际上返回了类似OnPreEventResult.Continue 的内容.我们已经制定了一些客户逻辑,我们希望 NHibernate 继续......所以 false 被返回

  1. to Continue:
    If we in the AuditEventListener return false, we in fact return something like OnPreEventResult.Continue. We've made some custome logic, and we want NHibernate to continue... so the false is returned

中断/中止:
Ayende 的例子向我们展示了如何将真正的 DELETE 更改为 UPDATE.显式调用更新 @event.Persister.Update(... 并且由于返回值 true,删除 执行,即 <代码>OnPreEventResult.Break

to Break/Abort:
Ayende's example is showing us how to change the real DELETE into UPDATE. Update is called explicitly @event.Persister.Update(... and the delete is not executed due to the returned value true, i.e. OnPreEventResult.Break

在代码中,返回值存储在名为 veto 的局部变量中,这再次更具自我描述性.

In the code, the returned values are stored in local variable called veto, which is again more self descriptive.

见:

来自 EntityInsertAction 的片段,Execute() 方法:

A snippet from the EntityInsertAction, the Execute() method:

...
bool veto = PreInsert();

if (!veto)
{    
    persister.Insert(id, state, instance, Session);
...

这篇关于假/真对 IPreInsertEventListeners 真正意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆