What false/true really mean for IPreInsertEventListeners?

后端 未结 1 1223
北荒
北荒 2020-12-19 18:27

I recently have found out how to audit instances with the IPreDeleteEventListener, IPreInsertEventListener and IPreUpdateEventListener

相关标签:
1条回答
  • 2020-12-19 19:21

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

    • 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. 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

    2. 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

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

    See:

    • EntityInsertAction, or
    • EntityDeleteAction

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

    ...
    bool veto = PreInsert();
    
    if (!veto)
    {    
        persister.Insert(id, state, instance, Session);
    ...
    
    0 讨论(0)
提交回复
热议问题