NHibernate

Relevant categories: Databases, Microsoft .NET

Among various methods for mapping the two worlds of “object oriented applications” and “relational databases”, ORM (Object Relational Mapping) has gained massive popularities in the software development community.

The idea of Hibernate was first developed in Java community, with the aim of enabling the developers to easily map the entities of a RBDMS to objects in an object oriented application environment. Upon its proven success a .NET port of this technology was developed by the .NET community and was named Nhibernate.

Nhibernate is a .NET solution for ORM (Object Relational Mapping) which frees the developers from huge amount of work for mapping a relational database to object oriented .NET classes and instances. Nhibernate is open source and is free for everyone to use.

By using Nhibernate it is easier to map data from relational database to object programming. There is no need for developers to get involved in the details of converting between objects of database and OO environment. Internally however, NHibernate uses dynamic code generation and meta programming (through .NET Reflection) to utilize ADO.NET objects for this purpose.

For using Nhibernate tool we should move through some steps:

  1. First of all we must define our data and create the tables in a RDBMS. For example, Microsoft SQL Server.
  2. Then we must define .NET classes with properties that represent each column of our database tables.
  3. The most important part is to define a mapping file so the Nhibernate can distinguish which class is related to which table and which property is related to which column. This file with the .hbm.xml extension is the guide for Nhibernate to map between our SQL tables and the .NET class.
  4. Nhibernate must know how and which database to connect to. For this reason we should define a connection configuration file for Nhibernate.
  5. The hard work is over. We can now start using Nhibernate, query and update data in the database by using NHibernate’s straightforward API.


Links: