Introduction to NHibernate for .NET

Summary

NHibernate is a sophisticated object/relational mapping (ORM) tool for .NET.

Object/relational mapping (ORM)

Object/relational mapping (ORM) is the automated persistence of classes (objects) in an application to the tables in a relational database, using metadata that describes the mapping between the classes (objects) and the database.

NHibernate maps classes in the application domain model against relational data stored in the database, using NHibernate XML mapping files with a .hbm.xml file extension. Usually one mapping file per class.

NHibernate can automatically save (persist) changes to data to the database in a way that is transparent to your domain model, by detecting which persistent objects have been modified by the application.

NHibernate compares objects by value, and not by object identity, to determine whether the object property's persistent state needs to be updated. (Any object with modifications that haven't yet been saved to the database is considered dirty.)

From an application perspective, a persistent class (object) is an in-memory representation of a particular row of a database table.

Advantages of Object/relational mapping (ORM)

  • Solves the problem of modelling mismatch - ORM allows object models to be mapped against relational models. Therefore, the object model does not have to adapt to match the underlying relational model, and prevents losing some of the advantages of object orientation.
  • Better productivity and maintainability - Reduces development time and improves maintainability due to less lines of code required.
  • Improved performance - ORM can perform better than ADO.NET when correctly used due to caching and batching features.
  • Creates database independence - Abstracts the application away from the underlying database being used.

More about NHibernate

NHibernate aims to be a complete solution to the problem of managing persistent data when working with relational databases and domain model classes.

NHibernate supports transparent transaction-level write-behind, which refers to the fact that NHibernate saves state changes to the database as late as possible and hides this detail from the application.

Using NHibernate results in fewer lines of code, which makes the application more understandable because there is more of an emphasis on business logic rather than application plumbing.

NHibernate can be configured to run in almost any .NET application and development environment.