Preface

Table of Contents

Where you can get these tutorials and its code
Requirements
Object / Relational Mapping

Overview

I will teach you here what Object / Relational Mapping (ORM) is and how to use NHibernate.

There are three big steps that we will folllow:

These tutorials are very long so it may take you lot of time to master them. I recommend you to practice as much as you can; test each feature by coding and executing some samples.

Where you can get these tutorials and its code

These tutorials are currently available in English only. You can find the online version here: http://nhibernate.sourceforge.net/NHibernateEg/. The French version will be released soon.

The source code is available on NHibernate CVS (in the "Documentation" branch) and currently, it can be downloaded here: http://nhibernate.sourceforge.net/NHibernateEg.zip.

Requirements

I expect you to have some basic knowledge in the development of database-based applications with the .NET framework. That is, working with a SQL database, creating a connection to it in a .NET Application, doing basic CRUD operations and manipulating transactions.

The applications need the .NET framework to be executed.

To modify and build the source code, you need the .NET SDK and NAnt or SharpDevelop or Visual Studio .NET.

Object / Relational Mapping

An Object / Relational Mapper is persistence layer connecting objects in an object-oriented system to data stored in a relational database. Using Object / Relational Mapping (ORM) allows one to cleanly apply object-oriented design, analysis, and programming techniques while hiding the specifics of dealing with the relational system.

ORM is very popular in the Java world; but even if it is still relatively unknown in the .NET world, there are already plenty of (commercial / open-source) libraries.

Do not confuse Object / Relational Mapping with Object / Role Modeling which is a fact-oriented method for performing information analysis at the conceptual level.

There are many kind of Object / Relational Mapping products:

  • Pure Relational. No ORM here; the application deals with tables and rows (using DataSet and/or DataReader).

  • Light Object Mapping. SQL queries are isolated/encapsulated as much as possible and some rows are manually converted to objects.

  • Medium Object Mapping. The application is designed with objects. This kind of products allows mapping collections and associations. And it is possible to do queries on objects.

  • Full Object Mapping. More advanced solution than Medium Object Mapping; it provides support for complex mapping like inheritance. It has a complete and object-oriented query API and advanced features like concurrency and caching strategies.

The step ahead might be to use a object/XML database; but it can require too much fundamental changes.

An Object / Relational Mapper uses internally many patterns and .NET features: Unit Of Work, Identity Map, Lazy Loading, Reflection, XML ...

For more details about ORM, read:

NHibernate provides Full Object Mapping.

From "NHibernate - Project of the Month, November 2005" on SourceForge.net:

NHibernate is a port of the excellent Java Hibernate relational persistence tool to the .NET platform. Hibernate is the leading solution for object-relational mapping (ORM) and object persistence in general for the Java platform. The NHibernate object persistence library for relational databases (version 1.0) is almost 100% feature-compatible with Hibernate 2.1. What makes NHibernate and Hibernate both unique is their approach to persistence, where your objects are not required to inherit from a special base class or implement an interface, (N)Hibernate works with plain old Java/CLR objects. Hibernate is also very flexible -- you can define how to persist individual properties and associations, query the database using a very concise and powerful query language, and so on.

What is the software's intended audience? Developers writing enterprise applications for the .NET platform, with a complex domain model. People who need a good ORM tool, and people who don't know what an ORM is yet. .NET developers who want to maintain domain model-centric applications backed by an RDBMS.

NHibernate - Project of the Month, November 2005.

NHibernate is production-ready; the version 1.0 was released in October 2005. And it is supported by JBoss Inc.