Data Access - Fluent Mappings Types and Inheritance

According to the types of inheritance the Data Access Fluent Mapping API offers a couple of diferent ways to model inheritance hierarchies – Vertical inheritance, Horizontal inheritance, Flat inheritance and Complex inheritance. For more information on the Fluent Mappings, see Fluent Mappings integration in Sitefinity.

Vertical Inheritance

This strategy is also known as "table-per-concrete-class" model. This means that each class is persistent in its own table. In Sitefinity the SitefinityProfile is inheriting the UserProfile via vertical inheritance. This means that the SitefinityProfile has its own table with name sf_sitefinity_profile and the UserProfile has its own table sf_user_profile. As the data persistent class SitefinityProfile inherits the UserProfile class, the fluent mapping is using this inheritance. The inheritance strategy is specified by using the Inheritance method for each of the derived types. For example:

var sitefinityProfileMapping = new MappingConfiguration<Telerik.Sitefinity.Security.Model.SitefinityProfile>();
        sitefinityProfileMapping.MapType(p => new { }).Inheritance(InheritanceStrategy.Vertical).SetTableName("sf_sitefinity_profile", this.Context);

NOTE: When implementing vertical inheritance, you have to specify a primary key only for the root class of the hierarchy, e.g. Id of the UserProfile.

Horizontal Inheritance

Horizontal inheritance can only be enabled for the topmost class in a hierarchy. Each immediate subclass is stored in its own table with a "copy" of the fields from the superclass. A horizontally mapped class itself, i.e. the superclass, does not have a table. Therefore, a horizontally mapped class cannot be directly persisted; only subclasses of the class can be stored in the database. For this reason, it is recommended (but not required) for horizontally mapped classes to be declared as abstract.

In Sitefinity the ContentBaseFluentMapping is built via Horizontal inheritance:

var contentMapping = new MappingConfiguration<Telerik.Sitefinity.GenericContent.Model.Content>();
        contentMapping.MapType(p => new { }).WithConcurencyControl(OptimisticConcurrencyControlStrategy.Version).Inheritance(InheritanceStrategy.Horizontal);

The reason is that this mapping is as a base class providing several fields like DateCreated, LastModified, Owner, Visible, etc. This class is not intended to be persisted in the database. Instead all it's inheritors will use the base properties defined by the class and define their own properties that are going to be mapped as fields in the database.

Flat Inheritance

Flat inheritance is the easiest and simplest way to model inheritance in the database. Fields from subclasses are mapped to a superclass table. In Flat ingeritance, the entire class hierarchy is persisted to a single table.

A sample of modelling Flat inheritance in Sitefinity is the TaxonomyFluentMapping:

private void MapTaxonomy(IList<MappingConfiguration> mappings)
        var taxonomyMapping = new MappingConfiguration<Telerik.Sitefinity.Taxonomies.Model.Taxonomy>();
        taxonomyMapping.MapType(p => new { }).WithConcurencyControl(OptimisticConcurrencyControlStrategy.Version).SetTableName("sf_taxonomies", this.Context);

private void MapFlatTaxonomy(IList<MappingConfiguration> mappings)
        var flatTaxonomyMapping = new MappingConfiguration<Telerik.Sitefinity.Taxonomies.Model.FlatTaxonomy>();
        flatTaxonomyMapping.MapType(p => new { }).Inheritance(InheritanceStrategy.Flat).SetTableName("sf_taxonomies", this.Context);

private void MapHierarchicalTaxonomy(IList<MappingConfiguration> mappings)
        var hierarchicalTaxonomyMapping = new MappingConfiguration<Telerik.Sitefinity.Taxonomies.Model.HierarchicalTaxonomy>();
        hierarchicalTaxonomyMapping.MapType(p => new { }).Inheritance(InheritanceStrategy.Flat).SetTableName("sf_taxonomies", this.Context);

Note how both the base class Taxonomy and the derived classes FlatTaxonomy(tags) and HierarchicalTaxonomy (categories) are mapped to the same table sf_taxonomies. Also all derived types must specify the Flat strategy mapping explicitly:


Now that you learned types of fluent mappings, in the next blog post, you will learn about the Fluent mappings associations.

Veronica Milcheva

About Veronica Milcheva

I am a passionate Sitefinity blogger, developer and consultant. In my spare time I enjoy running and listening to music. My personal quote: There's no tough problem, just not enough coffee :)

View Comments

comments powered by Disqus