Skip to main content
.NET Framework 4.5.2+

How to: Change Inheritance Mapping

  • 6 minutes to read

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 = "";

    // ...
}

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:

DefaultInheritance

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 = "";

    // ...
}

In the code example above, XPO will use a single table to store the inheritance hierarchy:

ParentTableInheriance

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