是否可以使用工厂方法来使用流畅的 nhibernate 创建类的实例?
例如,我有一组域类。这些类实现了一个公共接口。接口是公共的,但类本身是内部的。可以使用提供的公共工厂来创建这些类的实例。此外,每个类实例都使用每个层次一个表的技术来持久保存。
这种情况下如何使用Fluent NHibernate呢?任何帮助将不胜感激。
谢谢, 布莱克
For example, I have a set of domain classes. Those classes implement a common interface. The interface is public but the classes themselves are internal. Instances of those classes can be created by using a provided factory that is public. Furthermore, each class instance is persisted using a one-table-per-hierarchy technique.
How can Fluent NHibernate be used is this case? Any help would be greatly appreciated.
Thanks,
Blake
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这比 FNH 更适合 NHibernate,但据我所知,您有两个选择。
实现
IInterceptor.Instantiate(stringEntityName,EntityModeentityMode,object id)
- 您还必须将所述拦截器与您启动的任何NHibernate会话相关联。根据http://fabiomaulo.blogspot.com/2008/ 11/entities-behavior-injection.html ,子类
ReflectionOptimizer' 并实现
CreateInstance()` 方法(对于要为其构建工厂的每种类型都需要一个)。有一些配置和接线我不完全理解,我怀疑这基本上是方法 #1 的过度杀伤版本。如果您想为域类注入持久化字段和属性,我不确定是否有办法做到这一点,因为这是一个相当重要的问题。
This is more NHibernate specific than FNH, but you have two options as far as I can see.
Implement
IInterceptor.Instantiate(string entityName, EntityMode entityMode, object id)
- you would also have to associate said interceptor with any NHibernate sessions you start.As per http://fabiomaulo.blogspot.com/2008/11/entities-behavior-injection.html , subclass
ReflectionOptimizer' and implement the
CreateInstance()` method (need one for each type you want to build a factory for). There is some configuration and wiring I don't fully understand, and I suspect this is basically an overkill version of method #1.If you are wanting to inject persisted fields and properties for your domain class, I'm not sure if there is a way to do that as it is a rather non-trivial matter to generalise.
您还可以利用 NHibernate 2.0+ 中的事件系统。
Load
事件还允许您为 NHibernate 创建对象。您所要做的就是创建ILoadEventListener
的实现,将其添加到ISessionFactory
或ISession
的事件侦听器中,然后就可以获利!You can also take advantage of the event system in NHibernate 2.0+. The
Load
event allows you to create the object for NHibernate, as well. All you have to do is create an implementation ofILoadEventListener
, add it to the event listeners for theISessionFactory
orISession
, and profit!