Thursday, June 11, 2009

Object Relational Mapping ???


Object-Relational mapping is a programming technique that links databases to object-oriented language concepts, creating a "virtual object database." The package that implements the Object relational Mapping is known as Object Relational Mapper or ORM. ORM can be used in an n-tier application. There are many ORM packages available in the market both for free and commercial usage. However, some programmers develop their own ORM packages for the same purpose. This article would provide brief explanation about object relational mapping.
What is object Relational Mapping?
Definition: Object relational mapping is a technique converting data between incompatible type systems in relational databases and object-oriented programming languages.

When we are developing an object oriented application that requires a relational database we will need to have persistence objects that are modeled against your database design and to know how to interact with different tables in the database. We can either create them manually or use an Object relation mapping tool. When an object relational mapping tool is connected to a database, it allows you to map persistence objects to database tables and views, specify single row transactional operations, queries and stored procedure calls as methods to these objects. It allows you to define one to one, one to many, many to many relationships based on database design. It generates fully working persistent object code.
Object relational mapping is used when we want to take the advantage of transparent persistence and use a relational database. Relational database may be required because:
The data already resides in one or more relational databases
The data is new, but there are technical or political reasons for using a relational database
ORM – Object Relational Map per is a package that maps the objects to the relational tables and Tables columns to the object attribute one to one.
Example: We have an Employee Table with some columns in it. The figure below shows a business object of type Employee. It is not mandatory to have the employee declaration as shown below.
The Code written below is in C#. It typically explains a skeleton of business object.
Example code
Class Employee
{
public ID;
public Employee Name;
public Employee Salary;
}
The Employee class created needs to have a metadata that helps the ORM map to the table. There are many ORM packages available that deal with encapsulating the metadata with the class. We will look at the same in the next section.



Mapping terminology
• Mapping (v)-The act of determining how objects and their relationships are persisted in permanent data storage, in these case relational databases.
• Mapping (n)-The definition of how an object’s property or a relationship is persisted in permanent storage.
• property-A data attribute, either implemented as a physical attribute such as the string first Name or as a virtual attribute implemented via an operation such as get Total() which returns the total of an order
• Property mapping-A mapping that describes how to persist an object’s property.
• Relationship mapping-A mapping that describes how to persist a relationship (association, aggregation, or composition) between two or more objects.

Advantages of ORM
• ORM will reduce the programming code and through catching it will improve performance over using an embedded SQL or call-level interface with a relational database manager.
• A big advantage of mapping tools is that they provide an abstraction of the underlying database engine. Most of them allow switching easily between RDBMSs (Relational Database Management Systems).
• Automatic generation of SQL statements – The ORM layer generates automatically SQL statements. This helps the programmers concentrate more on the actual business logic removing the overhead of the SQL statements.
• Support of the Multiple Databases – Majority of the ORM packages available in the market support ANSI SQL standards. Hence, they can be used with most of the RDBMS. This can very useful in product development.
• ORM emphasizes on fully following the object oriented approach. Everything is an object. Database CRUD operations are typically called as a function. We will have functions on the business objects like Save, Update and Delete

Disadvantages of ORM
• Lack of standards – We have variety of ORM packages available in the market. We do not have common standard or conventions followed. Lack of such standard has made the programmers job tough. An ORM package may be completely different from another one.
• Performance related issues - Most of ORM related packages use reflection. As we know, using Reflection excessively is a performance overhead.


Compare toolsFeatures that a good ORM tool would provide

• Be able to use inheritance, create hierarchies between entities, and use polymorphism (we are using objects!). The tools can support a variety of combinations for tables and classes to allow these mechanisms.
• Handle any type of relations (1-1, 1-n, n-n)
• Support for transactions
• Aggregates (equivalent to SQL's SUM, AVG, MIN, MAX, COUNT)
• Support for grouping (SQL's GROUP BY)
• Static and dynamic queries
• Stored procedure calls
• Object catching
• Customization of generated code
• Template customization

No comments:

Post a Comment