NHibernate在单独的程序集中加入了子类

时间:2021-10-26 11:30:28

I have the following solution project structure:

我有以下解决方案项目结构:

Application.Core.Entities

Application.Xtend.CustomerName.Entities

In the Core project I have an entity Customer defiend. In the XTend project, I have an entity defined that subclasses Customer named xCustomer (for lack of a better name at this time...).

在核心项目中,我有一个实体客户定义。在XTend项目中,我定义了一个实体,它将Customer子类命名为xCustomer(此时缺少更好的名称......)。

The idea here is that we have a Core domain model in our application. A customer can then create a new assembly that contains extensions to our core model. When the extension assembly is present a smart IRepository class will return a subclass of the core class instead.

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

I am attempting to map this relationship in NHibernate. Using Fluent NHibernate I was able to generate this mapping:

我试图在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>

But NHib throws the following error:

但是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'..

NHibernate.MappingException:持久化类Application.Entites.xCustomer,找不到Application.Core.Entites ---> System.TypeLoadException:无法从程序集'Application.Core.Entites,Version = 1.0加载类型'Application.Entites.xCustomer' .0.0,Culture = neutral,PublicKeyToken = null'..

Which makes sense xCustomer is not defined in the Core library.

哪个有意义xCustomer未在Core库中定义。

Is it possible to span different assemblies like this? Am I approaching the problem wrong?

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

2 个解决方案

#1


7  

I asked this same question on the NHibernate Users mailing list and the solution was so obvious that I am somewhat embarrassed that I couldn't see it.

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

The hibernate-mapping attributes assembly and namespace are convenient short cuts that allow you to not have to fully qualify your class names. This lets you have the nice mark up , but the name attribute of both class and joined-subclass elements can take a fully qualified assembly name as well.

hibernate-mapping属性程序集和命名空间是方便的快捷方式,允许您不必完全限定类名。这使您可以获得良好的标记,但类和连接子类元素的name属性也可以采用完全限定的程序集名称。

So the above broken mapping file can be fixed like so:

所以上面破坏的映射文件可以像这样修复:

<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>

This works as I expected it to. So I then took a look at the Fluent-NHibernate source and created a patch complete with working unit tests to resolve the issue and submitted it to the project.

这符合我的预期。然后,我看了一下Fluent-NHibernate源代码并创建了一个补丁,其中包含工作单元测试以解决问题并将其提交给项目。

Thanks for you help @David Kemp

谢谢你帮助@David Kemp

#2


3  

You need to map using the extends attribute of the <class> element (AFAIK, this is new in NHibernate 2.0). Then you can have your subclass mapping (.hbm.xml) in the XTend assembly.

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

You might have to use the AddAttribute/AddProperty (can't remember what it's called) to do this using Fluent NHibernate. (Or submit a patch).

您可能必须使用AddAttribute / AddProperty(不记得它叫什么)来使用Fluent NHibernate来完成此操作。 (或提交补丁)。

#1


7  

I asked this same question on the NHibernate Users mailing list and the solution was so obvious that I am somewhat embarrassed that I couldn't see it.

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

The hibernate-mapping attributes assembly and namespace are convenient short cuts that allow you to not have to fully qualify your class names. This lets you have the nice mark up , but the name attribute of both class and joined-subclass elements can take a fully qualified assembly name as well.

hibernate-mapping属性程序集和命名空间是方便的快捷方式,允许您不必完全限定类名。这使您可以获得良好的标记,但类和连接子类元素的name属性也可以采用完全限定的程序集名称。

So the above broken mapping file can be fixed like so:

所以上面破坏的映射文件可以像这样修复:

<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>

This works as I expected it to. So I then took a look at the Fluent-NHibernate source and created a patch complete with working unit tests to resolve the issue and submitted it to the project.

这符合我的预期。然后,我看了一下Fluent-NHibernate源代码并创建了一个补丁,其中包含工作单元测试以解决问题并将其提交给项目。

Thanks for you help @David Kemp

谢谢你帮助@David Kemp

#2


3  

You need to map using the extends attribute of the <class> element (AFAIK, this is new in NHibernate 2.0). Then you can have your subclass mapping (.hbm.xml) in the XTend assembly.

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

You might have to use the AddAttribute/AddProperty (can't remember what it's called) to do this using Fluent NHibernate. (Or submit a patch).

您可能必须使用AddAttribute / AddProperty(不记得它叫什么)来使用Fluent NHibernate来完成此操作。 (或提交补丁)。