.NET HELP

NHibernate C# (How It Works For Developers)

Published June 6, 2024
Share:

NHibernate C# (How It Works For Developers): Figure 1 - Homepage of NHibernate C#

NHibernate is a powerful Object Relational Mapping (ORM) framework designed for use with the .NET framework. It provides developers with an efficient way to bridge the gap between the object-oriented world of .NET applications and the relational world of databases. By using NHibernate, you can significantly reduce the amount of boilerplate code required to implement data access layers, making your .NET applications cleaner and more maintainable.

The Role of ORM in Simplifying Database Interactions

ORM frameworks like NHibernate simplify interactions with relational databases by allowing developers to work with data in terms of objects and their properties rather than SQL statements. This abstraction helps developers to focus more on the business logic of their applications and less on the underlying SQL commands and database schema. For example, NHibernate handles all SQL generation and execution, allowing for operations like insertions, deletions, and updates to be conducted with simple object conversion and object manipulation.

Setting Up NHibernate in a .NET Project

To get started with NHibernate in your .NET project, the first step is to install the NHibernate package. This can be done easily through Visual Studio's NuGet Package Manager by using the following command:

Install-Package IronPdf

NHibernate C# (How It Works For Developers): Figure 2 - Open a command line console and input the command from above to install NHibernate

Configuring NHibernate with XML Configuration File

Once NHibernate is installed, the next step is to configure it. This involves creating a Hibernate mapping file that details your database server settings and the mapping details of your objects to the database tables. The main XML file, usually named hibernate.cfg.xml, includes settings such as the database connection string, dialect, and other database-specific settings.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">
      Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;
    </property>
    <property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
    <property name="show_sql">true</property>
    <mapping resource="Employee.hbm.xml"/>
  </session-factory>
</hibernate-configuration>
XML

Understanding NHibernate’s Core Components

One of the key components in NHibernate is the Session Factory, which is designed using the factory design pattern. This component creates Session objects that manage the connection to the database and hold the transactional operations. The Session Factory is costly to create, so it's typically done once per application lifetime, making it a crucial element for performance optimization.

Key Classes and Methods in NHibernate

NHibernate revolves around several essential classes and methods. For instance, the ISession interface plays a fundamental role in NHibernate, facilitating the creation of data query and manipulation sessions. Methods like OpenSession help developers start transactions, perform SQL commands, and query the database using either SQL statements or NHibernate’s own HQL (Hibernate Query Language).

Mapping Entities to Database Tables with NHibernate

Entity mapping in NHibernate is accomplished through mapping files, usually written in XML. These files, often named after the entity class (e.g., Employee.hbm.xml), define how an entity’s properties map to a database table's columns. A typical mapping file includes the class name, table name, and details about each property, including the primary key, column name, and data type.

Detailed Look at the Properties and Attributes Used in Mapping Files

In these mapping files, you can specify various attributes for each property, such as not-null constraints or unique constraints. NHibernate also allows for complex mappings like one-to-many and many-to-one relationships, providing a powerful tool set for representing relational data structures within an object-oriented framework.

Executing SQL Commands and Transactions in NHibernate

NHibernate simplifies CRUD (Create, Read, Update, Delete) operations by abstracting the underlying SQL commands. Developers can perform these operations without writing explicit SQL code, instead using methods provided by the ISession interface. For instance, to add a new entity to the database, you simply create a new instance of the object, set its properties, and use the Save method of the ISession.

Managing Transactions with ITransaction

Transactions in NHibernate are managed via the ITransaction interface, which ensures data integrity and consistency. Using the BeginTransaction method from ISession, developers can ensure that all operations are completed successfully before committing the data to the database, or roll back if something goes wrong, thereby maintaining the stability of your data.

Complete Code Example

This example includes the setup of the NHibernate configuration and mapping files and demonstrates how to perform Create, Read, Update, and Delete operations using NHibernate.

using NHibernate;
using NHibernate.Cfg;
using System;
public class Employee
{
    public virtual int Id { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
}
class Program
{
    private static ISessionFactory sessionFactory;
    static void Main()
    {
        sessionFactory = new Configuration().Configure().BuildSessionFactory();
        CreateEmployee();
        ReadEmployee(1);
        UpdateEmployee(1, "UpdatedName");
        DeleteEmployee(1);
    }
    static void CreateEmployee()
    {
        using (var session = sessionFactory.OpenSession())
        using (var transaction = session.BeginTransaction())
        {
            var newEmployee = new Employee
            {
                FirstName = "Iron",
                LastName = "Software"
            };
            session.Save(newEmployee);
            transaction.Commit();
            Console.WriteLine("Employee created: " + newEmployee.Id);
        }
    }
    static void ReadEmployee(int id)
    {
        using (var session = sessionFactory.OpenSession())
        {
            var employee = session.Get<Employee>(id);
            Console.WriteLine("Read Employee: " + employee.FirstName + " " + employee.LastName);
        }
    }
    static void UpdateEmployee(int id, string newFirstName)
    {
        using (var session = sessionFactory.OpenSession())
        using (var transaction = session.BeginTransaction())
        {
            var employee = session.Get<Employee>(id);
            employee.FirstName = newFirstName;
            session.Update(employee);
            transaction.Commit();
            Console.WriteLine("Employee updated: " + employee.FirstName);
        }
    }
    static void DeleteEmployee(int id)
    {
        using (var session = sessionFactory.OpenSession())
        using (var transaction = session.BeginTransaction())
        {
            var employee = session.Get<Employee>(id);
            session.Delete(employee);
            transaction.Commit();
            Console.WriteLine("Employee deleted");
        }
    }
}
using NHibernate;
using NHibernate.Cfg;
using System;
public class Employee
{
    public virtual int Id { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
}
class Program
{
    private static ISessionFactory sessionFactory;
    static void Main()
    {
        sessionFactory = new Configuration().Configure().BuildSessionFactory();
        CreateEmployee();
        ReadEmployee(1);
        UpdateEmployee(1, "UpdatedName");
        DeleteEmployee(1);
    }
    static void CreateEmployee()
    {
        using (var session = sessionFactory.OpenSession())
        using (var transaction = session.BeginTransaction())
        {
            var newEmployee = new Employee
            {
                FirstName = "Iron",
                LastName = "Software"
            };
            session.Save(newEmployee);
            transaction.Commit();
            Console.WriteLine("Employee created: " + newEmployee.Id);
        }
    }
    static void ReadEmployee(int id)
    {
        using (var session = sessionFactory.OpenSession())
        {
            var employee = session.Get<Employee>(id);
            Console.WriteLine("Read Employee: " + employee.FirstName + " " + employee.LastName);
        }
    }
    static void UpdateEmployee(int id, string newFirstName)
    {
        using (var session = sessionFactory.OpenSession())
        using (var transaction = session.BeginTransaction())
        {
            var employee = session.Get<Employee>(id);
            employee.FirstName = newFirstName;
            session.Update(employee);
            transaction.Commit();
            Console.WriteLine("Employee updated: " + employee.FirstName);
        }
    }
    static void DeleteEmployee(int id)
    {
        using (var session = sessionFactory.OpenSession())
        using (var transaction = session.BeginTransaction())
        {
            var employee = session.Get<Employee>(id);
            session.Delete(employee);
            transaction.Commit();
            Console.WriteLine("Employee deleted");
        }
    }
}
Imports NHibernate
Imports NHibernate.Cfg
Imports System
Public Class Employee
	Public Overridable Property Id() As Integer
	Public Overridable Property FirstName() As String
	Public Overridable Property LastName() As String
End Class
Friend Class Program
	Private Shared sessionFactory As ISessionFactory
	Shared Sub Main()
		sessionFactory = (New Configuration()).Configure().BuildSessionFactory()
		CreateEmployee()
		ReadEmployee(1)
		UpdateEmployee(1, "UpdatedName")
		DeleteEmployee(1)
	End Sub
	Private Shared Sub CreateEmployee()
		Using session = sessionFactory.OpenSession()
		Using transaction = session.BeginTransaction()
			Dim newEmployee = New Employee With {
				.FirstName = "Iron",
				.LastName = "Software"
			}
			session.Save(newEmployee)
			transaction.Commit()
			Console.WriteLine("Employee created: " & newEmployee.Id)
		End Using
		End Using
	End Sub
	Private Shared Sub ReadEmployee(ByVal id As Integer)
		Using session = sessionFactory.OpenSession()
			Dim employee = session.Get(Of Employee)(id)
			Console.WriteLine("Read Employee: " & employee.FirstName & " " & employee.LastName)
		End Using
	End Sub
	Private Shared Sub UpdateEmployee(ByVal id As Integer, ByVal newFirstName As String)
		Using session = sessionFactory.OpenSession()
		Using transaction = session.BeginTransaction()
			Dim employee = session.Get(Of Employee)(id)
			employee.FirstName = newFirstName
			session.Update(employee)
			transaction.Commit()
			Console.WriteLine("Employee updated: " & employee.FirstName)
		End Using
		End Using
	End Sub
	Private Shared Sub DeleteEmployee(ByVal id As Integer)
		Using session = sessionFactory.OpenSession()
		Using transaction = session.BeginTransaction()
			Dim employee = session.Get(Of Employee)(id)
			session.Delete(employee)
			transaction.Commit()
			Console.WriteLine("Employee deleted")
		End Using
		End Using
	End Sub
End Class
VB   C#

Database Portability and Interoperability Features

NHibernate is designed with database portability delivered. Thanks to its dialect configuration, NHibernate can adapt to most SQL databases with minimal changes to the codebase. This means you can switch from an SQL Server to MySQL or Oracle without having to rewrite your data access layer.

Adapting NHibernate for Various Database Systems Like SQL Server

The XML configuration files in NHibernate allow developers to specify the SQL dialect specific to their database system. This makes NHibernate a flexible solution that can be easily adapted to work with virtually any relational database that supports SQL, ensuring that your application is portable across different database systems.

Using NHibernate with IronPDF

NHibernate C# (How It Works For Developers): Figure 3 - Homepage of IronPDF

Integrating NHibernate with IronPDF is a powerful combination that can enhance your .NET applications. It allows you to manage database operations with NHibernate while leveraging IronPDF to generate PDF documents from your data. Consider a scenario where your application needs to provide user-specific documents, such as employee reports, which need to be generated and downloaded in PDF format. NHibernate C can efficiently manage the data retrieval processes from your database, while IronPDF can convert this data into well-formatted PDF files.

Install IronPDF

First, ensure IronPDF is added to your project. You can include it through the NuGet Package Manager by installing the IronPDF package.

Install-Package IronPdf
Install-Package IronPdf
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronPdf
VB   C#

NHibernate C# (How It Works For Developers): Figure 4 - Install IronPDF through NuGet Package Manager

Code Example

Let's delve deeper into how to implement this in your application. After setting up NHibernate and retrieving the necessary data from the database, such as employee details, you will prepare an HTML template that represents how the PDF document should appear. This HTML template can be dynamically filled with data obtained from NHibernate. For example, if you are generating a report for an employee, the template would include placeholders for the employee's name, ID, and other relevant details.

Here’s a detailed code example that demonstrates fetching data using NHibernate and converting it into a PDF using IronPDF:

using IronPdf;
using NHibernate;
static void CreateEmployeeReport(int employeeId)
{
    // Open a session to interact with the database
    using (var session = OpenSession())
    {
        // Retrieve the employee object based on the provided ID
        var employee = session.Get<Employee>(employeeId);
        // Create an instance of the HtmlToPdf class from IronPDF
        var renderer = new ChromePdfRenderer();
        // Create the HTML content for the PDF, embedding employee data into the HTML
        var htmlTemplate = $@"
            <html>
            <head>
                <title>Employee Report</title>
            </head>
            <body>
                <h1>Employee Details</h1>
                <p>Name: {employee.FirstName} {employee.LastName}</p>
                <p>ID: {employee.ID}</p>
            </body>
            </html>";
        // Render the HTML string as a PDF document
        var pdf = renderer.RenderHtmlAsPdf(htmlTemplate);
        // Save the generated PDF to a file
        pdf.SaveAs("EmployeeReport.pdf");
    }
}
using IronPdf;
using NHibernate;
static void CreateEmployeeReport(int employeeId)
{
    // Open a session to interact with the database
    using (var session = OpenSession())
    {
        // Retrieve the employee object based on the provided ID
        var employee = session.Get<Employee>(employeeId);
        // Create an instance of the HtmlToPdf class from IronPDF
        var renderer = new ChromePdfRenderer();
        // Create the HTML content for the PDF, embedding employee data into the HTML
        var htmlTemplate = $@"
            <html>
            <head>
                <title>Employee Report</title>
            </head>
            <body>
                <h1>Employee Details</h1>
                <p>Name: {employee.FirstName} {employee.LastName}</p>
                <p>ID: {employee.ID}</p>
            </body>
            </html>";
        // Render the HTML string as a PDF document
        var pdf = renderer.RenderHtmlAsPdf(htmlTemplate);
        // Save the generated PDF to a file
        pdf.SaveAs("EmployeeReport.pdf");
    }
}
Imports IronPdf
Imports NHibernate
Shared Sub CreateEmployeeReport(ByVal employeeId As Integer)
	' Open a session to interact with the database
	Using session = OpenSession()
		' Retrieve the employee object based on the provided ID
		Dim employee = session.Get(Of Employee)(employeeId)
		' Create an instance of the HtmlToPdf class from IronPDF
		Dim renderer = New ChromePdfRenderer()
		' Create the HTML content for the PDF, embedding employee data into the HTML
		Dim htmlTemplate = $"
            <html>
            <head>
                <title>Employee Report</title>
            </head>
            <body>
                <h1>Employee Details</h1>
                <p>Name: {employee.FirstName} {employee.LastName}</p>
                <p>ID: {employee.ID}</p>
            </body>
            </html>"
		' Render the HTML string as a PDF document
		Dim pdf = renderer.RenderHtmlAsPdf(htmlTemplate)
		' Save the generated PDF to a file
		pdf.SaveAs("EmployeeReport.pdf")
	End Using
End Sub
VB   C#

NHibernate C# (How It Works For Developers): Figure 5 - Example output from the code above

In this code, OpenSession() is a method that initializes an NHibernate session, which is used to fetch the employee data. The ChromePdfRenderer class from IronPDF then takes the HTML template filled with the fetched data and renders it as a PDF. This PDF is saved locally, but could also be streamed directly to a user through a web interface.

Conclusion

NHibernate C# (How It Works For Developers): Figure 6 - IronPDF licensing page

In this tutorial, we've explored how NHibernate simplifies database operations in .NET applications and how its integration with IronPDF enhances functionality by allowing for the generation of dynamic PDF documents. NHibernate provides robust data management tools, while IronPDF offers a convenient way to create professional-quality PDFs from HTML templates filled with data.

IronPDF is available for a free trial, and licenses begin at $749, offering a cost-effective solution for integrating powerful PDF generation into your applications. Together, these tools offer a comprehensive solution for managing data and producing documents, ideal for both enterprise-level and smaller-scale projects.

< PREVIOUS
Ocelot .NET (How It Works For Developers)
NEXT >
Rebus .NET Core Example (How It Works For Developers)

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 11,308,499 View Licenses >