NHibernate - Overview



In this chapter, we will discuss about what NHibernate is, which all platforms it can be implemented, what are its advantages and other aspects related to it.

What is NHibernate?

NHibernate is a mature, open source object-relational mapper for the .NET framework. It's actively developed, fully featured and used in thousands of successful projects. It's built on top of ADO.NET and the current version is NHibernate 4.0.4.

  • NHibernate is an open-source .NET object-relational mapper and is distributed under the GNU Lesser General Public License.

  • It is based on Hibernate which is a popular Java object-relational mapper and it has a very mature and active code base.

  • It provides a framework for mapping an object-oriented domain model to a traditional relational database.

  • NHibernate was started by Tom Barrett and this project has been around since February of 2003, which was their first commit.

  • It's a big project and provides a lot of functionality.

  • There is a NuGet package available, which makes it very easy to add to a project.

Why NHibernate?

Now the question is why do we need object-relational mappers? It is because there is a disconnect between the object world and the relational world.

  • In the object world, everything is based around objects; we called objects those things which have our data.

  • The relational world is all set-based and we are dealing with tables and rows which are different than the object world.

  • In the object world, we have unidirectional associations. If a customer has a pointer to an order, it doesn't necessarily mean that an order has a pointer back to a customer, it might or might not.

  • In the relational world, all associations are bidirectional and it can be done by a foreign key.

  • All associations are inherently bidirectional, so when we are dealing with object-relational mapping, we also need to deal with this disconnect.

  • In the object world, we are working with pointers that are unidirectional, whereas in with the relational world, we have foreign keys which are inherently bidirectional.

  • The object world has this notion of inheritance, where a vehicle can have a number of different subclasses, so a car is a type of vehicle, a boat is a type of vehicle, and a sports car is a type of car, these types of inheritance relationships.

  • The relational world doesn't have this notion of inheritance.

Mapping

So how do we map all these disjoint relationships? This concept of mapping comes from the object-relational mapper. There are mainly three things to understand as shown in the following diagram.

Mapping
  • In your application, you will need class definitions, which is typically C# code and its .NET code that represents our classes, such as Employee class, Customer class, Order class, etc.

  • At the bottom, you can see a database schema, which is our Data Definition Language in a relational database that specifies what a customer table looks like, what an employee table looks like.

  • In between these we have the mapping metadata that tells the object-relational mapper how to translate from the object world in C# to the database world in terms of rows and columns and foreign key relationships.

  • This mapping metadata can be represented in a variety of different ways and we will be looking at a number of this different ways most typical in NHibernate application.

  • It is represented by HBM (Hibernate Mapping) files, which are XML files.

Database Supported

NHibernate supports a wide variety of different databases. Any existing relational database out there can be accessed to NHibernate.

  • SQL server is the primary supported database, that's what most developers are using during the development, it's probably the most common one.

  • It also works very well with Oracle.

  • It also supports DB2, the Firebird, MySQL, PostgreSQL, SQL Lite

  • It also has ODBC and OLEDB drivers.

Advertisements