”;
Before we can really start using NHibernate, we need to understand the foundation on which it is built. NHibernate is a persistence technology that is based on the idea of object relational mapping or ORM.
What is ORM?
Object-Relational Mapping (ORM) is a programming technique for converting data between incompatible type systems in object-oriented programming languages. In other words, it is the concept of mapping an application”s business objects to relational database tables, so that the data can be easily accessed and updated entirely through the object model of an application.
-
As you already know that relational databases provide a good means of storing data, while object-oriented programming is a good approach to building complex applications.
-
NHibernate and ORM in general are most relevant to applications with nontrivial business logic, the domain model and some sort of database.
-
With ORM, it is very easy to create a translation layer that can easily transform objects into relational data and back again.
-
The acronym ORM can also mean object role modeling, and this term was invented before object/relational mapping became relevant.
-
It describes a method for information analysis, used in database modeling.
Why ORM?
ORM is a framework that enables you to map the world of objects found in object oriented languages to rows in relational tables found in relational databases
To understand this concept, let”s have a look at the following diagram.
-
In the above diagram, you can see that we have a table called Employee on the right side that contains columns with each piece of data associated with an individual employee.
-
We have a column for an Id which uniquely identifies each employee.
-
A column for the employee’s name, another column for their joining date, and finally a column that has an age of an employee.
-
If we wanted to write some code to store a new employee in the tables, it isn”t so easy.
-
In the above diagram, you can also see that we have an employee object that has fields for the Id, name, joining date and age.
-
Without an ORM we have to translate this object into a few different SQL statements that will insert the employee data into the employee table.
-
So writing code to create the SQL to do the above scenario is not that hard, but it is a bit tedious and pretty easy to get wrong.
-
Using an ORM like NHibernate, we can declare how certain classes should be mapped to relational tables and let the ORM or NHibernate deal with the nasty job of creating the SQL to insert, update, delete, in query data in our employee table.
-
This allows us to keep our code focused on using objects and have those objects automatically translated to relational tables.
-
So really what an ORM does is it saves us from manually having to map objects to tables.
”;