|
Inheritance in O/R Mapping |

Thursday, 01 February 2007
|
Posted by: Kashif Butt; Alachisoft; www.alachisoft.com Object oriented applications usually have inheritance as an important part of their design, including in their domain objects. However, the corresponding data model has no built-in mechanism for specifying inheritance. Therefore, you must map your domain objects to your relational database intelligently. Domain objects in an application represent the core data that is used by the application and therefore these objects are usually persisted in some data source. If the data source is a relational DBMS, then domain objects have to be mapped to a relational model which looks different from an object model. Therefore, there are many things that you must keep in mind when mapping these domain objects to a relational database. This article focuses on mapping inheritance in your domain objects to your relational databases. Mapping Inheritance As you know, inheritance represents an "IS-A" relationship between two classes where the derived class inherits all the characteristics of the base class. Incidentally, there is no direct inheritance relationship defined in the relational model. Therefore, when you map inheritance from your object model to your data model, you have two different ways in which you can map inheritance to a relational database. They are:
Employee is a base class whereas Engineer and Manager are derived classes. An Engineer is an Employee and so is a Manager. Therefore, both Engineer and Manager inherit all the attributes and methods of an Employee. The code skeleton of domain object classes for this model should look something like this.
Vertical Inheritance Mapping The simplest and most flexible inheritance mapping is where each class in the inheritance hierarchy (base or derived) is mapped to its own table in the database. And, each derived class table in the database has a one-to-one relationship with the base class table along with an existence dependency (meaning the derived class table cannot have a row unless there is a corresponding row in the base class table). The main benefit of this mapping is that it is very flexible and allows you to keep adding more derived classes without impacting any of the existing code in your application and also any existing tables in the database that are most likely holding a lot of valuable data. However, the drawback is that when you want to load a derived class many level down the hierarchy, the load involves a join of multiple tables (although the join is on primary keys) and is therefore a little slower. Additionally, when you do any insert, update, or delete operation, you end up making multiple database calls, one for each level of the inheritance hierarchy. Below is the data model for the above-mentioned object model.
In this data model, Employee class is directly mapped to t_employee table, Engineer is mapped to t_engineer, and Manager is mapped to t_manager. And, t_engineer and t_manager both have a one-to-one relationship with t_employee along with an existence dependency. Each attribute of Employee has a corresponding column in t_employee with which it is mapped and the same is true for Engineer and Manager. However, notice that Engineer or Manager does not have its own EmployeeId attribute but t_engineer and t_manager both have their own employee_id column. The Engineer and Manager actually use the EmployeeId from their base class Employee to store in t_engineer and t_manager tables. How Does Code Look? In vertical inheritance mapping, the code in the base class is totally unaware of the fact that there are derived classes and there is nothing special about it to mention here. This is also true to object oriented design principles. However, the code in derived classes is aware of inheritance and has to tackle one to one mapping as described below.
Code for CRUD Operations Let's see how one of the derived classes implements the CRUD methods. Below is Engineer class code.
The Insert method looks like this. Please note that Update is very similar to Insert except that a unique EmployeeId not generated.
Code for Queries Queries in base classes are not aware of class inheritance and therefore no special logic is required there. However, queries in a derived class with database mapping must always do a join between the base class table and the derived class table. This way, all the columns can be fetched and mapped to the attributes of both base and derived classes. Below is a query method for the Engineer class.
Conclusion Article Source: http://www.ArticleBlast.com |
||||||
Author: Kashif Butt works for Alachisoft, a leading software company providing O/R Mapping and Clustered Object Caching solutions for .NET. You can reach him at kashif@alachisoft.com or visit Alachisoft at www.alachisoft.com.
You are welcome to publish this article free of charge on your website, newsletter, or e-zine, provided:
- You don't change the article in any way
- You include the entire article, including the "about the author" box
- All hyperlinks must remain intact, including email addresses, and the link to ArticleBlast.com at the bottom
- In doing so you agree to indemnify the article's author, and ArticleBlast.com and its directors, officers, employees and agents from and against all losses, claims, damages and liabilities which arise out of its use
- It is also recommended that you provide a courtesy copy of your publication to the author of the article


