是否可以使用工厂方法来使用流畅的 nhibernate 创建类的实例?

发布于 2024-10-13 00:22:46 字数 157 浏览 2 评论 0原文

例如,我有一组域类。这些类实现了一个公共接口。接口是公共的,但类本身是内部的。可以使用提供的公共工厂来创建这些类的实例。此外,每个类实例都使用每个层次一个表的技术来持久保存。

这种情况下如何使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伊面 2024-10-20 00:22:46

这比 FNH 更适合 NHibernate,但据我所知,您有两个选择。

  1. 实现IInterceptor.Instantiate(stringEntityName,EntityModeentityMode,object id) - 您还必须将所述拦截器与您启动的任何NHibernate会话相关联。

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

  1. Implement IInterceptor.Instantiate(string entityName, EntityMode entityMode, object id) - you would also have to associate said interceptor with any NHibernate sessions you start.

  2. As per http://fabiomaulo.blogspot.com/2008/11/entities-behavior-injection.html , subclass ReflectionOptimizer' and implement theCreateInstance()` 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.

彻夜缠绵 2024-10-20 00:22:46

您还可以利用 NHibernate 2.0+ 中的事件系统。 Load 事件还允许您为 NHibernate 创建对象。您所要做的就是创建 ILoadEventListener 的实现,将其添加到 ISessionFactoryISession 的事件侦听器中,然后就可以获利!

 using NHibernate.Event.Default

 public class MyCreatorListener : DefaultLoadEventListener
 {
   // this is the single method defined by the LoadEventListener interface
   public override void OnLoad(LoadEvent theEvent, LoadType loadType)
   {
     if(null == theEvent.InstanceToLoad) // Not null if user supplied object
     {
       theEvent.InstanceToLoad = MyFactory.Create(loadType); // Or whatever.
     }
   }
 }

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 of ILoadEventListener, add it to the event listeners for the ISessionFactory or ISession, and profit!

 using NHibernate.Event.Default

 public class MyCreatorListener : DefaultLoadEventListener
 {
   // this is the single method defined by the LoadEventListener interface
   public override void OnLoad(LoadEvent theEvent, LoadType loadType)
   {
     if(null == theEvent.InstanceToLoad) // Not null if user supplied object
     {
       theEvent.InstanceToLoad = MyFactory.Create(loadType); // Or whatever.
     }
   }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文