从未映射的抽象中间类继承的连接子类

时间:2013-03-19 20:01:56

标签: c# nhibernate mapping-by-code

我有一个类概念上看起来类似的类层次结构:

enter image description here

也就是说,有一个抽象基类(Relation)和几个派生类。在实践中,CustomerSupplier共享了大量代码,因此我将共性重构为抽象类BusinessContact。现在实际的类层次结构如下所示:

enter image description here

或代码:

public abstract class Relation
{
    public virtual int Id { get; set; }
}

public class ContactPerson : Relation
{
    public virtual string PhoneNumber { get; set; }
}

public abstract class BusinessContact : Relation
{
    public virtual string Name { get; set; }
}

public class Customer : BusinessContact
{
    public virtual string CustomerNumber { get; set; } 
}

public class Supplier : BusinessContact
{
    public virtual string SupplierNumber { get; set; }
}

我想在NHibernate中使用join-subclasses将此层次结构映射到四个表(RelationContactPersonCustomerSupplier),使用mapping-by -code(ModelMapper)。我的映射看起来像这样:

var mapper = new ModelMapper();

mapper.Class<Relation>(map =>
{
    map.Id(x => x.Id, id => id.Generator(Generators.Native));
});

mapper.JoinedSubclass<ContactPerson>(map =>
{
    map.Key(key => key.Column("Id"));
    map.Property(x => x.PhoneNumber);
});

mapper.JoinedSubclass<Customer>(map =>
{
    map.Key(key => key.Column("Id"));
    map.Property(x => x.Name);
    map.Property(x => x.CustomerNumber);
});

mapper.JoinedSubclass<Supplier>(map =>
{
    map.Key(key => key.Column("Id"));
    map.Property(x => x.Name);
    map.Property(x => x.SupplierNumber);
});

但是,只要我尝试将映射添加到配置中,我就会遇到异常:

NHibernate.MappingException: Cannot extend unmapped class: BusinessContact

我基本上明白为什么会这样。生成的映射如下所示:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" namespace="JoinedSubClassMapping" assembly="JoinedSubClassMapping" xmlns="urn:nhib    ernate-mapping-2.2">
  <class name="Relation" abstract="true">
    <id name="Id" type="Int32">
      <generator class="native" />
    </id>
  </class>
  <joined-subclass name="ContactPerson" extends="Relation">
    <key column="Id" />
    <property name="PhoneNumber" />
  </joined-subclass>
  <joined-subclass name="Customer" extends="BusinessContact">
    <key column="Id" />
    <property name="CustomerNumber" />
  </joined-subclass>
  <joined-subclass name="Supplier" extends="BusinessContact">
    <key column="Id" />
    <property name="SupplierNumber" />
  </joined-subclass>
</hibernate-mapping>

CustomerSupplierBusinessContact属性中定义extends,就好像BusinessContact是模型中的“普通”实体一样。因为BusinessContact没有映射,所以失败了,或者当然。请注意,“名称”属性(在BusinessContact中定义)也不会出现在映射中。

想要的映射是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" namespace="JoinedSubClassMapping" assembly="JoinedSubClassMapping" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Relation" abstract="true">
    <id name="Id" type="Int32">
      <generator class="native" />
    </id>
  </class>
  <joined-subclass name="ContactPerson" extends="Relation">
    <key column="Id" />
    <property name="PhoneNumber" />
  </joined-subclass>
  <joined-subclass name="Customer" extends="Relation">
    <key column="Id" />
    <property name="CustomerNumber" />
    <property name="Name" />
  </joined-subclass>
  <joined-subclass name="Supplier" extends="Relation">
    <key column="Id" />
    <property name="SupplierNumber" />
    <property name="Name" />
  </joined-subclass>
</hibernate-mapping>

即,make SupplierCustomer扩展Relation并包含(否则未映射)BusinessContact类的所有映射属性。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

这是一个解决方案:

class MyInspector : ExplicitlyDeclaredModel {
    public override bool IsEntity(Type type) {
        if (type == typeof (BusinessContact))
            return false;
        return base.IsEntity(type);
    }
}

var mapper = new ModelMapper(new MyInspector());