c-sharp
  1. c-sharp-nhibernate-c

C# NHibernate

NHibernate is an open-source object-relational mapping (ORM) library for the .NET Framework. It provides a way to map object-oriented domain models to relational databases while preserving the flexibility and power of native SQL queries. In this tutorial, we'll discuss how to use NHibernate in a C# application.

Syntax

NHibernate uses its own syntax to define mappings between domain objects and database tables. Here's an example of a simple mapping:

<class name="Product" table="Products">
   <id name="Id" column="Id" type="Int32">
      <generator class="native" />
   </id>
   <property name="Name" column="Name" type="String" />
   <property name="Price" column="Price" type="Decimal" />
</class>

This mapping describes a simple Product object with an Id, Name, and Price property, mapped to a table called Products.

Example

Here's an example of how to use NHibernate in a C# application to perform CRUD operations:

using (ISession session = sessionFactory.OpenSession()) {
   using (ITransaction transaction = session.BeginTransaction()) {

      // Create a new product
      var product = new Product {
         Name = "Test Product",
         Price = 19.99M
      };
      session.Save(product);

      // Update the product
      product.Name = "Updated Product";
      session.Update(product);

      // Delete the product
      session.Delete(product);

      // Commit the transaction
      transaction.Commit();
   }
}

This example creates a new product, updates its name, then deletes it. The session object is used to perform the operations, with a transaction object to ensure consistency.

Output

The output of this example is the creation, update, and deletion of a product in the database.

Explanation

In the example above, we instantiated an NHibernate session factory and used it to create a new session object. We then began a transaction and used the session's Save method to create a new product object and save it to the database.

We then updated the product's name property and called the session's Update method to persist the changes to the database. Finally, we deleted the product object with the session's Delete method.

Once all the operations have been performed, we commit the transaction to finalize the changes in the database.

Use

NHibernate is commonly used in enterprise-level applications to simplify the interaction between the domain objects and the database. It is useful for reducing the amount of boilerplate code necessary to perform CRUD operations, and for providing a high level of abstraction between the application code and the database.

Important Points

  • NHibernate provides a wide range of features, such as support for lazy loading and caching of data.
  • NHibernate can be used with a variety of popular database management systems, including SQL Server, Oracle, MySQL, and PostgreSQL.
  • NHibernate mappings can be defined using XML files, annotations, or Fluent NHibernate.

Summary

In this tutorial, we discussed how to use NHibernate in a C# application. We covered the syntax, example, output, explanation, use, and important points of NHibernate in C#. With this knowledge, you can now use NHibernate to simplify the interaction between your C# application and the database, and reduce the amount of boilerplate code required to perform CRUD operations.

Published on: