Introduction to Object Relational Mapping with nHibernate (Part1)

Your Ad Here

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 have to write those hefty stored procedures to access database. 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. This is Edited !

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 doing 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 simple spelling problem. Not only that but migration can also be a big problem. If you have a stored procedure that takes 5 seconds to execute (Although 5 seconds is too long) and some one tweaked the procedure so now it runs in <>

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 and open source. 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.

Subscribe
Posted in Labels: , kick it on DotNetKicks.com |

2 comments:

  1. Anonymous Says:

    CopayCloraLer xaikalitag royapeCrord [url=http://uillumaror.com]iziananatt[/url] Siftlatasosse http://gusannghor.com Grivohara

  2. Unknown Says:

    Nice post very helpful
    DBAKings