Zum Fußzeileninhalt springen
.NET HILFE

NHibernate C# (Funktionsweise für Entwickler)

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 NHibernate

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 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;

// Define the Employee class with virtual properties
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()
    {
        // Initialize the SessionFactory using NHibernate configuration
        sessionFactory = new Configuration().Configure().BuildSessionFactory();

        // Perform database operations
        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); // Save the new Employee object to the database
            transaction.Commit(); // Commit the transaction to finalize the insertion
            Console.WriteLine("Employee created: " + newEmployee.Id);
        }
    }

    static void ReadEmployee(int id)
    {
        using (var session = sessionFactory.OpenSession())
        {
            // Retrieve the Employee object by its Id
            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())
        {
            // Get the Employee object by its Id
            var employee = session.Get<Employee>(id);
            employee.FirstName = newFirstName; // Update the employee's first name
            session.Update(employee); // Update the Employee object in the database
            transaction.Commit(); // Commit the transaction to save changes
            Console.WriteLine("Employee updated: " + employee.FirstName);
        }
    }

    static void DeleteEmployee(int id)
    {
        using (var session = sessionFactory.OpenSession())
        using (var transaction = session.BeginTransaction())
        {
            // Retrieve the Employee object to be deleted
            var employee = session.Get<Employee>(id);
            session.Delete(employee); // Delete the Employee from the database
            transaction.Commit(); // Commit the transaction to finalize the deletion
            Console.WriteLine("Employee deleted");
        }
    }
}
using NHibernate;
using NHibernate.Cfg;
using System;

// Define the Employee class with virtual properties
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()
    {
        // Initialize the SessionFactory using NHibernate configuration
        sessionFactory = new Configuration().Configure().BuildSessionFactory();

        // Perform database operations
        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); // Save the new Employee object to the database
            transaction.Commit(); // Commit the transaction to finalize the insertion
            Console.WriteLine("Employee created: " + newEmployee.Id);
        }
    }

    static void ReadEmployee(int id)
    {
        using (var session = sessionFactory.OpenSession())
        {
            // Retrieve the Employee object by its Id
            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())
        {
            // Get the Employee object by its Id
            var employee = session.Get<Employee>(id);
            employee.FirstName = newFirstName; // Update the employee's first name
            session.Update(employee); // Update the Employee object in the database
            transaction.Commit(); // Commit the transaction to save changes
            Console.WriteLine("Employee updated: " + employee.FirstName);
        }
    }

    static void DeleteEmployee(int id)
    {
        using (var session = sessionFactory.OpenSession())
        using (var transaction = session.BeginTransaction())
        {
            // Retrieve the Employee object to be deleted
            var employee = session.Get<Employee>(id);
            session.Delete(employee); // Delete the Employee from the database
            transaction.Commit(); // Commit the transaction to finalize the deletion
            Console.WriteLine("Employee deleted");
        }
    }
}
Imports NHibernate
Imports NHibernate.Cfg
Imports System

' Define the Employee class with virtual properties
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()
		' Initialize the SessionFactory using NHibernate configuration
		sessionFactory = (New Configuration()).Configure().BuildSessionFactory()

		' Perform database operations
		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) ' Save the new Employee object to the database
			transaction.Commit() ' Commit the transaction to finalize the insertion
			Console.WriteLine("Employee created: " & newEmployee.Id)
		End Using
		End Using
	End Sub

	Private Shared Sub ReadEmployee(ByVal id As Integer)
		Using session = sessionFactory.OpenSession()
			' Retrieve the Employee object by its Id
			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()
			' Get the Employee object by its Id
			Dim employee = session.Get(Of Employee)(id)
			employee.FirstName = newFirstName ' Update the employee's first name
			session.Update(employee) ' Update the Employee object in the database
			transaction.Commit() ' Commit the transaction to save changes
			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()
			' Retrieve the Employee object to be deleted
			Dim employee = session.Get(Of Employee)(id)
			session.Delete(employee) ' Delete the Employee from the database
			transaction.Commit() ' Commit the transaction to finalize the deletion
			Console.WriteLine("Employee deleted")
		End Using
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

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 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

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 ChromePdfRenderer 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 ChromePdfRenderer 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 ChromePdfRenderer 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
$vbLabelText   $csharpLabel

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 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.

Häufig gestellte Fragen

Wie kann ich NHibernate mit einer PDF-Generierungsbibliothek in C# integrieren?

NHibernate kann mit IronPDF integriert werden, indem NHibernate zur Handhabung von Datenbankoperationen und zum Abrufen von Daten verwendet wird, die IronPDF dann in PDF-Dokumente umwandeln kann. Dies ermöglicht die Erstellung dynamischer PDFs basierend auf benutzerspezifischen Daten.

Was ist der Zweck der Session Factory in NHibernate?

In NHibernate ist die Session Factory eine wichtige Komponente, die Session-Objekte erstellt, um Datenbankverbindungen zu verwalten und Transaktionsoperationen durchzuführen. Sie optimiert die Leistung, da ihre Erstellung kostspielig ist und typischerweise einmal pro Anwendungslebensdauer instanziiert wird.

Können Sie erklären, wie CRUD-Operationen in NHibernate durchgeführt werden?

CRUD-Operationen in NHibernate werden über das `ISession`-Interface abstrahiert, das Methoden wie Save, Update und Delete bereitstellt. Dies ermöglicht es Entwicklern, diese Operationen durchzuführen, ohne direkt SQL-Befehle schreiben zu müssen.

Was sind die Vorteile der Verwendung von NHibernate für .NET-Entwickler?

NHibernate bietet .NET-Entwicklern den Vorteil, den Aufwand für Boilerplate-Code in Datenzugriffsschichten zu reduzieren, was die Wartbarkeit von Anwendungen verbessert. Es abstrahiert Datenbankinteraktionen, sodass sich Entwickler mehr auf die Geschäftslogik konzentrieren können.

Wie unterstützt NHibernate die Datenbank-Portabilität?

NHibernate unterstützt die Datenbank-Portabilität durch seine Dialektkonfiguration, die es ihm ermöglicht, sich an verschiedene SQL-Datenbanken anzupassen. Dies ermöglicht es Entwicklern, von einem Datenbanksystem zu einem anderen mit minimalen Änderungen am Code zu wechseln.

Welche Rolle spielen Mapping-Dateien in NHibernate?

Mapping-Dateien in NHibernate, typischerweise XML-Dateien, definieren, wie die Eigenschaften einer Entität auf die Spalten einer Datenbanktabelle abgebildet werden. Sie umfassen wichtige Details wie Primärschlüssel, Spaltennamen und Datentypen und unterstützen komplexe Abbildungen wie Eins-zu-viele-Beziehungen.

Wie können Transaktionen in NHibernate effektiv verwaltet werden?

Transaktionen in NHibernate werden über das `ITransaction`-Interface verwaltet, das die Datenintegrität sicherstellt. Entwickler können die BeginTransaction-Methode von `ISession` verwenden, um Operationen zu handhaben, und Daten nur dann zu committen, wenn alle Operationen erfolgreich sind, oder zurückzusetzen, falls Probleme auftreten.

Wie richtet man NHibernate in einem .NET-Projekt ein?

Um NHibernate einzurichten, installieren Sie das NHibernate-Paket über Visual Studios NuGet Package Manager mit dem Befehl Install-Package NHibernate. Konfigurieren Sie es mit einer XML-Mapping-Datei wie `hibernate.cfg.xml`, um Datenbankeinstellungen und Objektabbildungen zu definieren.

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen