Building a Better ORM With NHIbernate For .NET

This article is for those people searching for an alternative to the Entity Framework. So, what is wrong with the Entity Framework? This article of mine will neither say anything negative about the Entity Framework (if any) nor the branding of Nhibernate. I am an evangelist for neither of the frameworks.
 
I like articles with less theory and more code. I have yet to reach this goal of mine. Bear with me this time.
 
If you are reading this term NHibernate for the first time, you have reasons to continue reading further. We will explain the following topics.
  • What Nhibernate is
  • Why NHibernate
  • Installing NHibernate
  • Creating the NHibernate mapping file
  • Querying database using NHibernate
  • References

NHibernate

 
Before explaining what it is, I recommend you read what ORM is (if you understand the term and its meaning). For those that have coded in Java for some years and jumped into .Net world, the term is joyful. NHibernate is a type of ORM from the hibernate family of Java. Simply the hibernate version of .Net ORM is Nhibernate.
 
“NHibernate is a mature, open-source Object – Relational Mapper for .NET Framework.”
 
When there is a need for your .Net application (Windows, web, or whatever) to communicate with the database, we can use it. NHibernate is a very nice product but the least marketed in the crowd. Famous products like Resharper, JIRA, and nantbuilder, and so on sponsor this every year.
 

Why NHibernate

 
Products around the globe are becoming smart every day along with the people who develop them. Each product must satisfy each objective. If we are developing a product like Visual Studio, we cannot sell it to Java developers even though Visual Studio is good. Visual Studio targets the .Net developing crowd. Apply a similar example to our topic. Let us use Entity Framework for example. Entity Framework is a very nice ORM and I was working with it in my past application. With Entity Framework you will need to install it, do the necessary mapping procedure, and at the end, you have a .edmx file ready to proceed with CRUD operations. This does not satisfy me personally at all.
 
I wanted to do all the mapping I wanted on my own from the scratch. So I need to move to NHibernate to make my mapping files fully under control.
 

Installing NHibernate

 
NHibernate has two ways to install it. One, with a Zip file containing mapping files and DLLs. The other, a Nuget package.
 
Let us talk about the Zip file first, here are the instructions:
  1. Download the file from website.
  2. After extracting and opening the file you can see the Required_bins folder containing the DLLs and other mapping files.
  3. Reference the DLLs that are available in this folder to your application.
  4. Add the namespace using.NHibernate and using NHibernate.cfg to the class that needs mapping.
Installing a NuGet does not need much explanation. In the package manager console type “install-package nhibernate”.
 
Once NHibernate is downloaded, it must be referenced as specified in Step 4.
 
Creating a NHibernate mapping file
 
The initial step is to create an XML mapping file that lets the classes containing POCO to talk to the database. You can even first go ahead with creating classes for mapping.
 
I have created a class containing POCO and named it as Customer.cs.
 
NHibernate
 
I am creating a simple XML file for mapping named Customer.hbm.xml. Here .hbm.xml states that it is a nhibernate mapping file. Here I am defining a hibernate-mapping tag along with the assembly and the namespace. I am creating a mapping file based on these classes and properties for the mapping.
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataTransfer" namespace="DataTransfer">  
  3.   <class name="DataTransfer.Customer" table="Customer">  
  4.     <id name="CustomerId" column="CustomerId" type="Int32" unsaved-value="0">  
  5.       <generator class="native"></generator>  
  6.     </id>  
  7.     <property name="FirstName" column="FirstName" type="string" length="50" not-null="false"></property>  
  8.     <property name="LastName" column="LastName" type="string" length="50" not-null="false"></property>  
  9.   </class>  
  10. </hibernate-mapping> 
In order to map this perfectly with our database, we need to create a customer table containing firstname, lastname, and customerid. The preceding mapping file has the following attributes.
  • Hibernate-mapping: This is the starting tag having the hibernate mapping namespace. It has several attributes and some of them are assembly and namespace.
  • Class: Contains the class name for the specific application. It has important attributes like name and table. Here name describes the name of the class and the table defines the name of the database table.
  • Id: determines the id attributes we use in our tables. It has subtags like generator. The generator tag has attributes like native, hilo, guid.comb, guid, and so on. I must keep this article as short as possible. Google will help you learn about them.
  • Property: determines the column names of the tables. Here the attribute "name" represents the name of the property in customer.cs and the column attribute determines the name of the column.
Querying database using NHibernate
 
There are several ways to write queries in Nhibernate. They are:
  1. Using LINQ: You can use LINQ to query.
  2. HQL: acronym for Hibernate Query Language, the same as LINQ but with slight differences.
  3. Using Criteria API: An API to make querying easier with extension methods and lambda expressions.
  4. QueryOver: another API to make querying easy.
Attached is a sample project file for your reference. The application will get feeling comfortable with the basic idea of what Nhibernate is. Please also read about FluentNHibernate. FluentNhibernate is flexible and there are very many plugins available to make mapping easier in NHibernate.
 
References
A Sample application can be downloaded here. There is no need to provide any login information and payment for the download. Download as a regular user in the browser.


Similar Articles