What is ORM?
ORM stands for Object Relational Mapping. It is a technique that lets you map objects to relational databases and vice versa. Here, the term "object" mostly means an instance of a class. Tools that make use of ORM techniques are also referred to as ORM, which stands for Object Relational Mapper. NHibernate is one such tool. Let's take a look at a very simple example in order to understand what exactly this technique does. Suppose that you have the following Customer
class:
public class Customer { public string FirstName {get; set;} public string LastName {get; set;} public string EmailAddress {get; set;} public int Age {get; set;} }
Tip
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books that you have purchased. If you have purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Every customer will be represented by an instance of the preceding class in the memory. Every such instance will also be persisted in the database. Suppose that the following database table is used to store customer instances:
CREATE TABLE tblCustomer ( FirstName NVARCHAR(100), LastName NVARCHAR(100), EmailAddress NVARCHAR(100), Age INT );
The preceding syntax is for the MS SQL Server database. If you are using a different RDMBS, then the syntax may differ.
Depending on which ORM you are using, you will have a way to tell your ORM that the Customer
class corresponds to the tblCustomer
table in your database. Similarly, you can tell that the FirstName
, LastName
, EmailAddress
, and Age
properties map to columns with the same names in the tblCustomer
table. Once all of this is in place, you can just tell your ORM to save an instance of the Customer
class; it will ensure that a record is inserted into the tblCustomer
table with the appropriate values for all the columns.
In a nutshell, that is ORM for you. ORM will handle all your database CRUD operations for you without you having to write a single line of SQL script. However, the most important principle to understand around an ORM is that it tries to bridge the gap between the OO world and the relational world. Programs written using the object-oriented principles adhere to a set of rules and support a particular set of data types. On the other hand, most RDBMSs follow rules derived from the set theory and support a set of data types that may not all be compatible with the corresponding data type on the OO side of the world. Besides this, there are differences in how new objects are constructed, how they associate with each other, what kind of operations are permitted on them, and so on.
All these differences make working with databases difficult when viewed from the perspective of the OO program. These differences are also called impedance mismatch, a term taken from electrical engineering. Impedance gives a measure of how easily the current can flow through an electrical circuit. For two electrical circuits to work in coherence with each other when connected, their impedances should match. In the same way, for a program written in OOP to work with an RDBMS in coherence, the impedance between them has to be matched. An ORM does this job.