对IPreInsertEventListeners来说,假/真到底意味着什么? [英] What false/true really mean for IPreInsertEventListeners?

查看:108
本文介绍了对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而不是Ayende所写的false.

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.

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 时,动作中止
  • 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.由于返回值 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, or
  • EntityDeleteAction

来自EntityInsertActionExecute()方法的代码段:

...
bool veto = PreInsert();

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

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

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