NHibernate:拦截器和监听器之间的区别
查看 NHibernate 中创建/更新列的所有可能性,我主要是(Stackoverflow 问题, Ayende Rahien)通过监听器查看解决方案。
在我公司进行编程的程序员使用拦截器来实现同样的事情。
这两种解决方案有什么区别吗? (其中之一是否已过时,其中之一是首选,优点和/或缺点是什么)
Looking at all the possibilites of creation / update columns in NHibernate I mostly (Stackoverflow question, Ayende Rahien) see solutions with Listeners.
The programmer who was programming this in my company used an Interceptor to achieve the same thing.
Is there any difference between those two solutions? (Is one of them obsolete, is one of them preferred and what are the advantages and / or disadvantages)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
拦截器是旧的方式,事件侦听器是较新的并且服务于相同的目的。 所以在新项目中,推荐使用事件监听器。 您连接到新的NHibernate 事件系统。
编辑:正如 Rashack 在评论中提到的,有些操作只能使用拦截器来完成。
Interceptors are the the old way, event-listeners are newer and server the same purpose. So in a new project, event-listeners are recommended. You hook up to the new NHibernate event system.
Edit: As Rashack mentions in his comment, there are some operations that can only be done using the interceptor.
根据文档,它指出:
“如果您必须对持久层中的特定事件做出反应,您还可以使用 Hibernate 事件架构。事件系统可以另外使用,或者作为拦截器的替代品。”
两者有一些区别:
拦截器可以通过编程和声明性方法在会话范围内或会话工厂范围内。 另一方面,以声明方式注册的侦听器无法共享实例。 如果在多个元素中使用相同的类名,则每个引用都将产生该类的单独实例。 如果您需要在侦听器类型之间共享侦听器实例,则必须使用编程注册方法。
As per documentation, it states that :
"If you have to react to particular events in your persistence layer, you can also use the Hibernate event architecture. The event system can be used in addition, or as a replacement, for interceptors. "
There are a few differences in both :
Listeners are easily configurable i.e. as per hibernate doc "Having the type additionally defined during registration makes it easier to turn custom listeners on or off during configuration"
Interceptors can be session-scoped or session factory scoped through programmatic as well as declarative approach. On the other hand, Listeners registered declaratively cannot share instances. If the same class name is used in multiple elements, each reference will result in a separate instance of that class. If you need to share listener instances between listener types you must use the programmatic registration approach.