2013-05-13 147 views
0

我有两个类,一个从另一个继承。第一个显示数据的概要视图(4列以上),而孩子显示详细视图(40列以上)。这两个类都访问同一个表并共享被访问的相同列。Nhibernate并继承按代码映射

我的孩子类可以从父类继承,所以我只需要在一个地方更改映射?我宁愿没有重复的代码运行猖獗。

如:

Public Class Parent 
Public Overridable Property MyProp As String 
Public Overridable Property MyProp2 As String 
End Class 

Public Class Child : Inherits Parent 
Public Overridable Property MyProp3 As String 
End Class 

我想要做这样的事情:

Public Class ParentMapping 
    Inherits ClassMapping(Of Parent) 
Public Sub New() 
Me.Property(Function(x) x.MyProp, Sub(y) y.column("MyProp")) 
Me.Property(Function(x) x.MyProp2, Sub(y) y.column("MyProp2")) 
End Sub 
End Class 

Public Class ChildMapping 
Inherits SubClassMapping(of Child) 
Public Sub New() 
    ' I want to be able to inherit the mappings of the parent here. 
    MyBase.New() 

    Me.Property(Function(x) x.MyProp3, Sub(y) y.column("MyProp3")) 
End Sub 
End Class 

回答

1

如果你希望孩子成为分贝还父的子类,你需要一个鉴别列。

如果你只是想重用代码然后共享的映射基类

public abstract class ParentChildMapping<T> : ClassMapping<T> where T : Parent 
{ 
    public ParentChildMapping() 
    { 
    // map shared properties here 
    } 
} 

public class ParentMapping : ParentChildMapping<Parent> 
{ 
} 

public class ChildMapping : ParentChildMapping<Child> 
{ 
    public ChildMapping() 
    { 
    // map additional properties here 
    } 
} 
+0

这与我在做什么,但NHibernate的似乎回到孩子时,我的域范围内指定的父对象的对象。 NHibernate的查询包括只在子对象上的属性... – ps2goat 2013-05-15 14:48:13

+0

到目前为止,我做了一些更改并且它可以工作。我会回报其他问题。 – ps2goat 2013-05-15 16:32:14

+0

当你将Parent映射为基类时,它当然会返回所有Parent对象,并且Child对象也是Parent对象。 – Firo 2013-05-16 05:31:52