NHibernate is an Object-Relational Mapping (ORM) framework for the .NET platform that allows developers to map domain objects to a relational database. It is a port of the Java-based Hibernate ORM framework and provides similar functionality for .NET developers. This article will provide an overview of NHibernate and how it can be used with C#.
What is NHibernate?
NHibernate is an ORM framework that allows developers to work with a domain model in a more natural way, using objects to represent data rather than SQL statements. NHibernate takes care of the mapping between the objects and the database so that developers can focus on the business logic of the application.
NHibernate is built on top of the ADO.NET framework, which provides the low-level connectivity to the database. NHibernate uses a set of configuration files to configure the database connections, mappings, and other settings. The configuration files are used to specify the database dialect, connection strings, and other settings.
NHibernate provides a rich set of features including:
The Core Components of NHibernate Architecture are as Follows:
It is the entry point to NHibernate, responsible for creating Session objects. It is a thread-safe object that is created once during the application startup.
It is a lightweight object that is used to interact with the database. The Session object represents a single unit of work, where a unit of work is a set of operations that need to be executed together.
It is used to configure NHibernate at the application startup. The configuration object is used to specify the connection string, database dialect, mapping files, and other settings.
4. Mapping files:
NHibernate uses mapping files to map between object-oriented code and relational databases. Mapping files specify how each object and its properties are mapped to the database tables and columns.
How does NHibernate Work?
NHibernate uses mapping files to define the relationship between the domain model and the database. These mapping files describe how the properties of the domain objects are mapped to the database schema. NHibernate supports two types of mapping files:
XML-based mapping files, which are defined in an XML format.
Fluent Mapping, which uses C# code to define the mapping between domain objects and the database.
Here's an example of how NHibernate mapping works. Let's say we have a Customer class with the following properties:
We can use mapping files to define how this class is mapped to the database. Here's an example of an XML-based mapping file for the Customer class:
This mapping file defines the mapping between the Customerclass and the Customerstable in the database. It specifies that the Id property is the primary key for the table and that the FirstName, LastName, and Email properties are mapped to the columns with the same name in the database.
NHibernate also supports Fluent Mapping, which allows developers to define the mapping between domain objects and the database using C# code. Here's an example of how to define the mapping for the Customer class using Fluent Mapping:
This Fluent Mapping code defines the same mapping as the XML-based mapping file. It specifies that the Customerclass is mapped to the Customerstable in the database and that the properties are mapped to the columns with the same name.
NHibernate also provides a session object that manages the interaction between the domain objects and the database. The session object is responsible for creating, retrieving, updating, and deleting domain objects and managing transactions and concurrency. The session object is lightweight and is designed to be short-lived. It should be created when needed and then closed as soon as the work is done.