Nhibernate and inheriting mapping-by-code

I have two classes, one inheriting from the other. The first shows a summary view of data (4+ columns) and the child shows a detail view (40+ columns). Both classes are accessing the same table and share the same columns being accessed.

Can my child class inherit from the parent class so I only have to change mappings in one place? I'd rather not have duplicate code running rampant.

E.g.:

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

I want to do something like this:

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

Answers


If you want the Child to be a subclass of parent in db also, you'll need a discriminator column.

If you just want to reuse code then share a mapping base class

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

Need Your Help

activesupport-2.3.11 conflicts with activesupport (>= 3.0)

ruby-on-rails ruby gem legacy

I am working on a Ruby on Rails project that a former programmer created. It is old code, using Ruby 1.8.7 and Rails 2.3.*. I am not going to upgrade versions because all I need to do is add a few

Rencode AVI file to fit within given output file size.

video-encoding avi divx

I have an AVI file, which I want to re-encode to fit on a CD of size 650 MB. How can I encode such that file size does not exceed the given size? Which program can I use ?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.