Domanda

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
È stato utile?

Soluzione

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
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top