功能NHibernate:如何映射与谁的类型是接口的属性的实体? [英] Fluent nhibernate: How do I map an entity with a property who's type is an interface?

查看:123
本文介绍了功能NHibernate:如何映射与谁的类型是接口的属性的实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个实体:

public class Employee
{
    public int ID { get; set; }
    public IAccountManager AccountManager { get; set; }
    ...
}



我也有一个映射定义DefaultAccountManager - 一个具体实施IAccountManager的。当映射上面的雇员的实体了,我怎么告诉NHibernate的持久化/加载使用DefaultAccountManager定义的映射中的AccountManager财产

I also have a mapping defined for "DefaultAccountManager" - a concrete implementation of IAccountManager. When mapping the above "Employee" entity, how do I tell NHibernate to persist/load the AccountManager property using the mapping defined in "DefaultAccountManager"?

修改?
其实如果我能为建立一个IAccountManager映射,这样NHibernate的可能只是推断要加载的实施者/持续,这将是更加美好。我宁愿没有通过强制所有实施者使用相同的映射打破多态性。

Actually if I could setup a mapping for IAccountManager so that NHibernate could just infer which implementer to load/persist that would be even better. I'd rather not have to break polymorphism by forcing all implementers to use the same mapping.

推荐答案

我没有找到答案这个。我在细节上一点点朦胧的,因为它是在几个月前,但下面是该解决方案的JIST:

I did find an answer to this one. I'm a little hazy on the details, as it was a few months ago, but the following was the jist of the solution:


  • 对于IAccountManager的每个实现具有映射创建一个表。

  • 确保你的数据库是设置为使用希洛ID算法。

  • 使用工会子类在你的映射

联盟的子类会是这个样子:

Union-subclasses would look something like this:

<class name="IAccountManager" abstract="true">
  <id name="ID" column="ID" type="Int32">
    <generator class="hilo"/>
  </id>
  <union-subclass name="DefaultAccountManager" table="DefaultAccountManager"
      proxy="IAccountManager">
    <property name="FirstName" type="String"/>
    <property name="LastName" type="String"/>
  </union-subclass>
  ... more implementations
</class>

请注意在联盟的子类属性名。这应该是(和匹配)IAccountManager的每个实现独一无二的。

Note the attribute "name" on union-subclass. This should be unique for (and match) each implementation of IAccountManager.

另外,标识,而不是被唯一的每个表中,将是唯一的所有IAccountManagers(通过利用希洛)。

Also, the ID, instead of being unique to each table, will be unique to all IAccountManagers (by leveraging hilo).

当NHibernate的看到一个IAccountManager实体,它将使用实例的具体类型和工会子类定义,以找出正确的表。

When NHibernate sees an IAccountManager entity, it will use the instance's concrete type and the union-subclass definitions to figure out the correct table.

希望这有助于!

这篇关于功能NHibernate:如何映射与谁的类型是接口的属性的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆