xpo-2064-examples-how-to-change-inheritance-mapping.md
XPO fully supports inheritance and polymorphism of persistent objects, that is you can retrieve a collection of base objects and then deal with the actual instances of concrete classes.
public class Person: XPObject {
public string PhoneNumber {
get { return fPhoneNumber; }
set { SetPropertyValue(nameof(PhoneNumber), ref fPhoneNumber, value); }
}
string fPhoneNumber = "";
}
public class Contact: Person {
public string FirstName {
get { return fFirstName; }
set { SetPropertyValue(nameof(FirstName), ref fFirstName, value); }
}
string fFirstName = "";
public string LastName {
get { return fLastName; }
set { SetPropertyValue(nameof(LastName), ref fLastName, value); }
}
string fLastName = "";
public string Email {
get { return fEmail; }
set { SetPropertyValue(nameof(Email), ref fEmail, value); }
}
string fEmail = "";
public Company Employer {
get { return fEmployer; }
set { SetPropertyValue(nameof(Employer), ref fEmployer, value); }
}
Company fEmployer;
// ...
}
public class Company: Person {
public string Name {
get { return fName; }
set { SetPropertyValue(nameof(Name), ref fName, value); }
}
string fName = "";
public string WebSite {
get { return fWebSite; }
set { SetPropertyValue(nameof(WebSite), ref fWebSite, value); }
}
string fWebSite = "";
// ...
}
Public Class Person : Inherits XPObject
Public Property PhoneNumber() As String
Get
Return fPhoneNumber
End Get
Set(ByVal value as String)
SetPropertyValue(NameOf(PhoneNumber), fPhoneNumber, value)
End Set
End Property
Private fPhoneNumber As String = ""
End Class
Public Class Contact : Inherits Person
Public Property FirstName() As String
Get
Return fFirstName
End Get
Set(ByVal value as String)
SetPropertyValue(NameOf(FirstName), fFirstName, value)
End Set
End Property
Private fFirstName As String = ""
Public Property LastName() As String
Get
Return fLastName
End Get
Set(ByVal value as String)
SetPropertyValue(NameOf(LastName), fLastName, value)
End Set
End Property
Private fLastName As String = ""
Public Property Email() As String
Get
Return fEmail
End Get
Set(ByVal value as String)
SetPropertyValue(NameOf(Email), fEmail, value)
End Set
End Property
Private fEmail As String = ""
Public Property Employer() As Company
Get
Return fEmployer
End Get
Set(ByVal value as Company)
SetPropertyValue(NameOf(Employer), fEmployer, value)
End Set
End Property
Private fEmployer As Company = Nothing
' ...
End Class
Public Class Company : Inherits Person
Public Property Name() As String
Get
Return fName
End Get
Set(ByVal value as String)
SetPropertyValue(NameOf(Name), fName, value)
End Set
End Property
Private fName As String = ""
Public Property WebSite() As String
Get
Return fWebSite
End Get
Set(ByVal value as String)
SetPropertyValue(NameOf(WebSite), fWebSite, value)
End Set
End Property
Private fWebSite As String = ""
' ...
End Class
In the code example above, the Contact and Company classes are based on the Person class. By default, XPO will store descendant-specific properties in separate tables and it will automatically build table joins to retrieve object data:
If you want to store the properties of a descendant class in a base class table, you can change the inheritance mapping type to ParentTable. In this instance, XPO will store all properties from all the descendants in the same table.
public class Person: XPObject {
public string PhoneNumber {
get { return fPhoneNumber; }
set { SetPropertyValue(nameof(PhoneNumber), ref fPhoneNumber, value); }
}
string fPhoneNumber = "";
}
[MapInheritance(MapInheritanceType.ParentTable)]
public class Contact: Person {
public string FirstName {
get { return fFirstName; }
set { SetPropertyValue(nameof(FirstName), ref fFirstName, value); }
}
string fFirstName = "";
public string LastName {
get { return fLastName; }
set { SetPropertyValue(nameof(LastName), ref fLastName, value); }
}
string fLastName = "";
public string Email {
get { return fEmail; }
set { SetPropertyValue(nameof(Email), ref fEmail, value); }
}
string fEmail = "";
public Company Employer {
get { return fEmployer; }
set { SetPropertyValue(nameof(Employer), ref fEmployer, value); }
}
Company fEmployer;
// ...
}
[MapInheritance(MapInheritanceType.ParentTable)]
public class Company : Person {
public string Name {
get { return fName; }
set { SetPropertyValue(nameof(Name), ref fName, value); }
}
string fName = "";
public string WebSite {
get { return fWebSite; }
set { SetPropertyValue(nameof(WebSite), ref fWebSite, value); }
}
string fWebSite = "";
// ...
}
Public Class Person : Inherits XPObject
Public Property PhoneNumber() As String
Get
Return fPhoneNumber
End Get
Set(ByVal value as String)
SetPropertyValue(NameOf(PhoneNumber), fPhoneNumber, value)
End Set
End Property
Private fPhoneNumber As String = ""
End Class
<MapInheritance(MapInheritanceType.ParentTable)> _
Public Class Contact : Inherits Person
Public Property FirstName() As String
Get
Return fFirstName
End Get
Set(ByVal value as String)
SetPropertyValue(NameOf(FirstName), fFirstName, value)
End Set
End Property
Private fFirstName As String = ""
Public Property LastName() As String
Get
Return fLastName
End Get
Set(ByVal value as String)
SetPropertyValue(NameOf(LastName), fLastName, value)
End Set
End Property
Private fLastName As String = ""
Public Property Email() As String
Get
Return fEmail
End Get
Set(ByVal value as String)
SetPropertyValue(NameOf(Email), fEmail, value)
End Set
End Property
Private fEmail As String = ""
Public Property Employer() As Company
Get
Return fEmployer
End Get
Set(ByVal value as Company)
SetPropertyValue(NameOf(Employer), fEmployer, value)
End Set
End Property
Private fEmployer As Company = Nothing
' ...
End Class
<MapInheritance(MapInheritanceType.ParentTable)> _
Public Class Company : Inherits Person
Public Property Name() As String
Get
Return fName
End Get
Set(ByVal value as String)
SetPropertyValue(NameOf(Name), fName, value)
End Set
End Property
Private fName As String = ""
Public Property WebSite() As String
Get
Return fWebSite
End Get
Set(ByVal value as String)
SetPropertyValue(NameOf(WebSite), fWebSite, value)
End Set
End Property
Private fWebSite As String = ""
' ...
End Class
In the code example above, XPO will use a single table to store the inheritance hierarchy:
You can specify different inheritance mapping types for members of the same hierarchy. This technique gives you a great degree of control over data store layout in your database.
For developers working with persistent objects, both types of inheritance mapping look similar - it does not affect object behavior in any way. The only reason you will want to deal with the inheritance mapping type attribute is to fine-tune the database object store, segmentation and performance.
Note
By making the base class non-persistent you can implement inheritance mapping with which all the attributes, including inherited, are stored in the table which corresponds to a descendant class. The only exception of this implementation is that you cannot reference the base class in other persistent objects.
See Also