2008-11-03 48 views
8

我有以下解决方案项目结构:NHibernate的在单独的组件连接子类

Application.Core.Entities

Application.Xtend.CustomerName.Entities

核心项目我有一个实体客户 defiend。在XTend项目中,我有一个实体定义了Customer的子类xCustomer(因为目前缺乏更好的名称)。

这里的想法是,我们在我们的应用程序中有一个核心域模型。然后客户可以创建一个包含我们核心模型扩展的新程序集。当扩展程序集存在时,智能类将返回核心类的子类。

我试图映射这种关系NHibernate。使用Fluent NHibernate我能够产生这种映射:

<?xml version="1.0" encoding="utf-8"?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
        default-lazy="false" 
        assembly="NHibernate.Core.Entites" 
        namespace="NHibernate.Entites" 
        default-access="field.camelcase-underscore"> 
    <!-- Customer is located in assembly Application.Core.Entities --> 
    <class name="Customer" table="Customers" xmlns="urn:nhibernate-mapping-2.2"> 
    <id name="Id" column="Id" type="Int64"> 
     <generator class="native" /> 
    </id> 
    <component name="Name" insert="true" update="true"> 
     <property name="LastName" column="LastName" length="255" type="String" not-null="true"> 
     <column name="LastName" /> 
     </property> 
     <property name="FirstName" column="FirstName" length="255" type="String" not-null="true"> 
     <column name="FirstName" /> 
     </property> 
    </component> 
    <!-- xCustomer is located in assembly Application.XTend.CustomerName.Entities --> 
    <joined-subclass name="xCustomer" table="xCustomer"> 
     <key column="CustomerId" /> 
     <property name="CustomerType" column="CustomerType" length="255" type="String" not-null="true"> 
     <column name="CustomerType" /> 
     </property> 
    </joined-subclass> 
    </class> 
</hibernate-mapping> 

但NHib引发以下错误:

NHibernate.MappingException: persistent class Application.Entites.xCustomer, Application.Core.Entites not found ---> System.TypeLoadException: Could not load type 'Application.Entites.xCustomer' from assembly 'Application.Core.Entites, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'..

这是合理的xCustomer未在核心库中定义。

是否有可能跨越这样的不同组件?我接近问题了吗?

回答

7

我在NHibernate Users邮件列表上问了同样的问题,解决方案非常明显,我有些尴尬,我看不到它。

hibernate-mapping属性程序集和名称空间是方便的捷径,它允许您不必完全限定类名。这可以让你有很好的标记,但class和joined-subclass元素的name属性也可以采用完全限定的程序集名称。

所以上面的破映射文件可以固定像这样:

<joined-subclass name="Application.XTend.CustomerName.Entities.xCustomer, 
       Application.XTend.CustomerName.Entities, Version=1.0.0.0, 
       Culture=neutral, PublicKeyToken=null" 
       table="xCustomer"> 
    <key column="CustomerId" /> 
    <property name="CustomerType" column="CustomerType" length="255" 
      type="String" not-null="true"> 
    <column name="CustomerType" /> 
    </property> 
</joined-subclass> 

这个工程,我期望它。于是我看了一下Fluent-NHibernate源代码,并创建了一个补丁,完成了工作单元测试以解决问题,并且submitted it to the project

感谢您的帮助@David Kemp

3

您需要使用<class>元素的extends属性(AFAIK,这是NHibernate 2.0中的新增功能)映射。然后,您可以在XTend程序集中映射您的子类(.hbm.xml)。

你可能不得不使用AddAttribute/AddProperty(不记得它叫什么)来使用Fluent NHibernate来做到这一点。 (或提交补丁)。