简体   繁体   English

NHibernate:拦截器的意义返回值

[英]NHibernate: Meaning of interceptors return value

I think this is an easy question, but my googling is weak on this. 我认为这是一个简单的问题,但我的谷歌搜索在这方面很弱。

I had the problem described in the following link with regard to a generated ID and cascading: 关于生成的ID和级联,我遇到了以下链接中描述的问题:

https://www.hibernate.org/hib_docs/nhibernate/html/example-parentchild.html (towards the bottom) https://www.hibernate.org/hib_docs/nhibernate/html/example-parentchild.html (向下)

I fixed it using their suggested method of an Interceptor. 我使用他们建议的Interceptor方法修复它。 Everything appears to be working, so I am happy. 一切似乎都在起作用,所以我很开心。

That said, I have no idea what the significance of the return value is from methods such as: 也就是说,我不知道返回值的重要性来自以下方法:

    public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, IType[] types)
    {
        if (entity is Persistent) ((Persistent)entity).OnLoad();
        return false;
    }

    public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)
    {
        if (entity is Persistent) ((Persistent)entity).OnSave();
        return false;
    }

In both cases false is returned. 在这两种情况下都会返回false。

When I google about NHibernate Interceptors I see plenty of examples of how to write one. 当我谷歌关于NHibernate拦截器时,我看到了很多关于如何编写一个的例子。 Some instead return true ( http://www.lostechies.com/blogs/rhouston/archive/2008/03/27/creating-a-timestamp-interceptor-in-nhibernate.aspx ). 有些反而返回true( http://www.lostechies.com/blogs/rhouston/archive/2008/03/27/creating-a-timestamp-interceptor-in-nhibernate.aspx )。 I have no idea what the difference is here. 我不知道这里的区别是什么。 My code is working, but Interceptors seem useful to me so I'd like to have a better understanding. 我的代码正在运行,但拦截器对我来说似乎很有用,所以我希望能有更好的理解。

I believe the return value should indicate if the state parameter has been changed in the interceptor method. 我相信返回值应该指示拦截器方法中是否已更改状态参数。 You're right - it's a tough one to google at the moment - the NHibernate site moved recently and google doesn't seem to find as much useful info as it used to. 你是对的 - 这对谷歌来说是一个艰难的 - 最近NHibernate网站移动了,谷歌似乎没有找到那么多有用的信息。

Huey, 休伊,

Read this post, i'm not a .net programmer, but this post is very usefull: 阅读这篇文章,我不是.net程序员,但这篇文章非常有用:

http://knol.google.com/k/fabio-maulo/nhibernate-chapter-11/1nr4enxv3dpeq/14# http://knol.google.com/k/fabio-maulo/nhibernate-chapter-11/1nr4enxv3dpeq/14#

Chapter 11. Interceptors and events It is often useful for the application to react to certain events that occur inside NHibernate. 第11章拦截器和事件应用程序对NHibernate内部发生的某些事件做出反应通常很有用。 This allows implementation of certain kinds of generic functionality, and extension of NHibernate functionality. 这允许实现某些类型的通用功能,以及NHibernate功能的扩展。

11.1. 11.1。 Interceptors The IInterceptor interface provides callbacks from the session to the application allowing the application to inspect and/or manipulate properties of a persistent object before it is saved, updated, deleted or loaded. 拦截器IInterceptor接口提供从会话到应用程序的回调,允许应用程序在保存,更新,删除或加载之前检查和/或操纵持久对象的属性。 One possible use for this is to track auditing information. 一个可能的用途是跟踪审计信息。 For example, the following IInterceptor automatically sets the createTimestamp when an IAuditable is created and updates the lastUpdateTimestamp property when an IAuditable is updated. 例如,以下IInterceptor在创建IAuditable时自动设置createTimestamp,并在更新IAuditable时更新lastUpdateTimestamp属性。

Hope it enlight you a little more. 希望它能让你更加开心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM