O/R Mappings Series Articles – Part I by azamsharp

Series Part I

Abstract:

This is an article series in which I will talk about using O/R Mapping. O/R Mapping can ease your life since you don’t need to write those hefty stored procedures. O/R Mapping will also put an end to using datasets in your applications. In the first part of the article I will just discuss that why we need O/R Mapping and what it is all about. The fun part (coding) and seeing mapping in action will start from the next article.

Introduction:

We all are familiar with datasets which is a carrier used to carry data from one layer to another. In my previous article I described some of the pitfalls of using datasets. Datasets are most commonly used when we are accessing database. The common pattern of accessing data from the database is given in the following steps:
1) Make connection to the database
2) Make DataAdapter
3) Execute commands using dataadapter or command objects using stored procedures or Ad-hoc queries.
4) Fill the dataset using dataadapter
5) Bind dataset to the control on the webform.
The above procedure does not look that long but consider does this over and over and over again. What will happen when some database field you misspelled and now its occurring in 200 stored procedures. You need to tell your Boss that you need a week or even more to fix this spelling problem. Not only that but migration can also be a big problem. If you have a stored procedure that takes 5 seconds to run (Although 5 seconds is too long) and some one tweaked the procedure so now it runs in < 1 second and you are planning to use the new procedure throughout your application since its better in performance but later you find out that old stored procedure occur in 100 places than what are you doing to do?. Actually to be honest I ran into this issue just one week ago. When I had to upgrade the data access layer of an application from “Data Access Application Block” to “Enterprise Library Data Access Block”. Data Access was used more than 200 places in the whole application so I had to go to each and every place and manually change the access to use Enterprise Library. This migration was done in 2 days which was less than I expected but what will happen when Microsoft will introduce a new Data Access Library which will be different from the Enterprise Library. We have to repeat the whole procedure again and again.
What if I told you that you can insert, delete, update, edit in other words perform all type of database operations without executing any SQL at all. This seems impossible and kind of a crazy thought but this is what O/R Mapping is all about. O/R Mapping also known as Object Relationship Mapping which allows us to map our data in file(s) and use those mapping files to retrieve or add data to the database. The main idea behind the O/R Mappings is relationships. Consider the following query:

SELECT Person.Name, Person.Email FROM Person,Customer 
WHERE Person.PersonID = Customer.CustomerID

As you can see that Person table and the Customer table has relationship between them which let’s them perform different actions. Just in the same way O/R mapping is also relationships and these relationships can be created in files.
There are many O/R Mappers available to download. Some are free and some are pretty expensive. In this article and for the rest of the series we will be looking at the NHibernate O/R Mapper which is a free open source application. You can download the NHibernate O/R Mapper from http://nhibernate.sourceforge.net/ .
Here is a list of different O/R Mappers (List of O/R Mappers)

When not to use O/R Mapping:

You should not use O/R Mapping when your business logic is dependent on complex stored procedures. Having said so if you have business logic implemented in stored procedures than you have another thing coming :). Almost 95% of all the stored procedures that you write can be represented by O/R Mapping.

Installing NHibernate:

Installation is pretty simple. I assume you downloaded the Zip file from http://sourceforge.net/project/showfiles.php?group_id=73818. Once you done downloading simply extract it on your hard drive. It will have different folders inside the main directory, some of those folders contains the code and some have different type of files. Your concern will be the .dll files in the bin directory.
After installing the NHibernate just sit back and wait for my next article in which I will show you how can you access the database and how cool it is to use O/R Mapping.