2008-11-03 18 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來做到這一點。 (或提交補丁)。