g.Single Table Inheritance

  1. Motivation
    1. Represents an inheritance hierarchy of classes that maps to a single database table having all columns for all the fields of the various classes.
  2. Summary
    1. Relational databases don't support inheritance, so when mapping from objects to databases we have to consider how to represent our nice inheritance structures in relational tables.
    2. When mapping to a relational database, we try to minimize the joins that can quickly mount up when processing an inheritance structure in multiple tables.
    3. Single Table Inheritance maps all fields of all classes of an inheritance structure into a single table.
    4. This pattern is mainly used while designing domain model using some ORM framework like EF.

  3. When to Use
    1. Single Table Inheritance is one of the options for mapping the fields in an inheritance hierarchy to a relational database. 
    2. Use this when there is some field that are relevant only for certain rows of table such as salary is relevant only to full time employee and the is a category type field that can clearly groups all rows in different segments. These segments are maps to a single class.
    3. When the above process is repeated again then we get multilevel inheritance Hierarchy 
  4. Related Patterns
    1. Class Table Inheritance
    2. Concrete Table Inheritance
  5. Related Technologies 
    1. Entity Framework 
  6. Specific Considerations 
    1. Pros and Cons of Single Table Inheritance 
      1. Pros  
        1. There's only a single table to worry about on the database. 
        2. There are no joins in retrieving data. 
        3. Any refactoring that pushes fields up or down the hierarchy doesn't require you to change the database. 
      2. Cons 
        1. Fields are sometimes relevant and sometimes not, which can be confusing to people using the tables directly. 
        2. Columns used only by some subclasses lead to wasted space in the database.
        3. The single table may end up being too large, with many indexes and frequent locking, which may hurt performance. 
        4. You can avoid this by having separate index tables that either list keys of rows that have a certain property or that copy a subset of fields relevant to an index.
  7. References
    1. http://martinfowler.com/eaaCatalog/singleTableInheritance.html