开发者

NHibernate PreUpdateEvent send Veto back to User Interface

Background

Trying to switch from using Interceptor to Events.

I have a ValidationInterceptor that overrides OnFlushDirty and OnSave and runs my custom validation logic. If the entity has validation errors, an exception is thrown which makes its way back to the UI.

My new ValidationHandler which implements IPreUpdate and IPreInsert EventListeners does the same valid开发者_StackOverflow中文版ation logic, but returns "true" to indicate to NHibernate to veto the operation. Which does work, but the UI does not know that the veto occurred.

Question

How does the UI know that a veto occurred during a PreInsert or PreUpdate event?

I tried to throw an exception from those events, but had the effect of allowing the save to occur and the exception did not make it back to the UI.

All the information the UI has points to a successful save:

  • the id of the entity has been set.
  • checking the session IsDirty() is false.
  • no exceptions during save attempt.


Oh I am dumb.

Let me present my code for proof:

public bool OnPreInsert(PreInsertEvent @event)
{
  try
  {
    var entity = @event.Entity as Api.IValidatable;
    if (entity == false)
      return false;

    if (entity.HasErrors())
      throw new ObjectIsInvalidException(entity.ValidationErrorMessage());

    return false;
  }
  catch (Exception ex)
  {
    log.Error("Unable to perform PreInsert validation on entity", ex);
  }
}

Yup, the exception was being swallowed before it ever made it out of the method.

Changed to rethrow the exception and all is working as it should.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜