October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Using NHibernate as an ORM Solution for .NET

  • November 6, 2007
  • By David Consdorf
  • Send Email »
  • More Articles »

NHibernate is an ORM (Object-Relational Mapping) solution for .NET that provides an easy to use framework for mapping an object-oriented domain model to a traditional relational database. Handling the myriad of database queries and stored procedures for large applications can become a time consuming endeavor for any application developer. NHibernate helps ease that pain by relieving developers from a significant amount of relational data persistence-related programming tasks. In this article, I will discuss ORM concepts and NHibernate's implementation of them. I will also walk you through several examples of NHibernate in use.

What Is Object-Relational Mapping?

Object-Relational Mapping (ORM) is the concept of mapping an application's business objects to relational database tables so that data can be accessed and updated entirely through the object model of an application.

Many popular controls in .NET allow you simply to bind data directly from the database to your controls. This helps allow programmers to quickly set up and create simple applications with .NET. But, what happens when your application starts growing? New business logic is added over time and makes things much more complicated. The database becomes bigger and more complex with each new requirement and it soon starts to take on a life of its own. With each change you make to your application, existing functionality starts to break because the database and front-end are tightly coupled and changing one will affect the other. Soon, you find yourself in a maintenance nightmare with an application that is more band-aid than actual functionality.

Any developer or system architect who has worked with an application after the initial construction phase understands this problem well. One of the best solutions to avoiding a maintenance-nightmare application is a good multi-layered N-tier architecture and adherence to solid object-oriented design principles.

Object-Relational Mapping helps achieve this end by providing a framework for mapping data from a database into application business objects. When you want to access or save data, you simply retrieve the object or save the object representing the needed data. The basic idea is to abstract the database access from the rest of the application by encapsulating the database access within its own layer and representing the data from the database within business objects as opposed to working directly with fields from the database.

How does this help solve the aforementioned maintenance-nightmare application scenario? The biggest benefit comes from the organization of the application into discrete areas of concern, otherwise known as loose coupling. Changing the database only requires a change to the object mapping. Because the rest of the application doesn't communicate with the database and only communicates with the business objects, the rest of the application is unaffected by the change. This organization doesn't only help maintenance scenarios. It is also very useful for keeping the development of large enterprise applications manageable because project teams more easily can split up into areas of concern. One or two people can develop the database model and business object mappings while the remainder of the team concerns itself with working with the business objects and their encapsulated functionality to develop the rest of the application.

What Is NHibernate?

NHibernate is an open source ORM (Object-Relational Mapping) solution for .NET. For several years, Hibernate (notice the dropping of the "N") has been making a name for itself in the Java world as a premier ORM solution for everyday database woes. Hibernate is a proven technology and is currently the standard approach to database architecture for enterprise applications for many top companies around the world.

With such a great solution, it was only a matter of time before Hibernate was ported to .NET and thus NHibernate was born. Although NHibernate is newer to the market and has not made as deep a penetration as its Java counterpart, NHibernate has all the major ORM features of Hibernate and is a solid and viable solution for ORM in the .NET world.

NHibernate implements the ORM concepts by providing a framework to map database tables to business objects using a standard XML format for each mapping and the database configuration. NHibernate is database independent, which means that the mappings are the same whether you are using a MS SQL, Oracle, DB2, or MySQL database. NHibernate also provides mapping solutions for a variety of object-oriented concepts such as inheritance, polymorphism, and the .NET collections framework including generic collections.

How to Implement NHibernate

For this article, I will introduce you to the framework and show you a small sample web application that touches on several of NHibernate's key principles.

Note: NHibernate is a rather large framework and this sample is far from a complete example of all of Hibernates functionality, but it is a good start.

Download the example code in this article here.

Application Overview

The application you will build is a small customer ordering system. You will model your business objects around the concept of people and orders. There will be two types of people, customers and salespeople. Customers will have a list of their orders and salespeople will have a list of the customers they are assigned to. Each order will have one customer and each customer may be assigned to one or more salespeople. Here is the object model:

public partial class Person
{
   private int? _personID;
   private string _firstName;
   private string _lastName;

   // Property Definitions...
}
public partial class Customer : Person
{
   private string _address;
   private string _city;
   private string _state;
   private string _zipCode;
   private ISet _orders;

   // Property Definitions...
}
public partial class SalesPerson : Person
{
   private string _employeeNumber;
   private ISet _assignedCustomers;

   // Property Definitions...
}
public partial class ProductOrder
{
   private int? _productOrderID;
   private int? _customerID;
   private string _productName;
   private int _quantity;
   private decimal _totalCost;

   // Property Definitions...
}

Listing 1: NHibernate Sample Application—Object Model





Page 1 of 6



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel