0

I just can't decide on which approach to choose to be able to have a hierarchial tree with partially different object types. The application is Asp.Net MVC and I'm using entity framework code first. My hierarchial tree is an information content structure. Each node in the tree can be of either one of my three implemented types.

For now, Table-per-type (TPT) seems to be my best choice of design but what do you think? Any drawbacks with my choice of design? Should the base class have a discriminator field, for example?

To illustrate, below's an pseudo example of my current design.

Regards, Clas Ericson

    public abstract class PageObject
    {
        public int ID { get; set; }
        public int? ParentID { get; set; }
        public string Title { get; set; }
        public virtual PageObject Parent { get; set; }
        public virtual ICollection<PageObject> Children { get; set; }
    }

    [Table("LinkPage")]
    public class LinkPage : PageObject
    {
        public string Url { get; set; }
    }   

    [Table("FramedPage")]
    public class FramedPage : LinkPage
    {
        public int? MinHeight { get; set; }
        public int? MinWidth { get; set; }
    }   

    [Table("ContentPage")]
    public class ContentPage : PageObject
    {
        [DataType(DataType.Html)]
        [AllowHtml]
        public string Content { get; set; }
    }   

public class InheritanceMappingContext : DbContext
{
    public DbSet<PageObject> PageObjects { get; set; }
}

1 Answer 1

1

Should the base class have a discriminator field, for example?

You don't have to declare it explicitly while using any of these approaches. Discriminator field will be automatically added as a table field by Entity Framework when using TPH approach, because it stores everything in 1 table, and this field acts like an pointer to which class this record belongs.

For now, Table-per-type (TPT) seems to be my best choice of design but what do you think?

You should realize, that this is completely opinion-based question, moreover it depends on your needs. Each approach has it's advantages and vice versa.

TPH will grant you best performance, because all the data is selected by 1 query without any JOINS, but it requires properties in subclasses to be Nullable in the Database. Also TPH violates the third normal form (price for performance)

The primary advantage of TPT strategy is that the SQL schema is normalized but performance can be unacceptable for complex class hierarchies because queries always require a join across many tables

http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code-first.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

I think I'll give TPT a shot. My "normalized" mind doesn't like breaking normalization forms and as you said, there's nothing directly wrong with that approach either. If performance is going down while testing I guess TPH will be the rescue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.