- Motivation
- Represents an inheritance hierarchy of classes that maps to a single database table having all columns for all the fields of the various classes.
- Summary
- 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.
- 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.
- Single Table Inheritance maps
all fields of all classes of an inheritance structure into a single table.
- This pattern is mainly used while designing domain model using some ORM framework like EF.
- When to Use
- Single Table Inheritance is one of the options for mapping the fields in an inheritance hierarchy to a relational database.
- 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.
- When the above process is repeated again then we get multilevel inheritance Hierarchy
- Related Patterns
- Class Table Inheritance
- Concrete Table Inheritance
- Related Technologies
- Entity Framework
- Specific Considerations
- Pros and Cons of Single Table Inheritance
- Pros
- There's only a single table to worry about on the database.
- There are no joins in retrieving data.
- Any refactoring that pushes fields up or down the hierarchy doesn't require you to change the database.
- Cons
- Fields are sometimes relevant and sometimes not, which can be confusing to people using the tables directly.
- Columns used only by some subclasses lead to wasted space in the database.
- The single table may end up being too large, with many indexes and frequent locking, which may hurt performance.
- 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.
- References
- http://martinfowler.com/eaaCatalog/singleTableInheritance.html
|
|