What is NHibernate and how is it different from Entity Framework?

13 augustus 2021 om 10:00 by ParTech Media - Post a comment

While developing any software or a product, seamless access to databases plays a very critical role. Therefore it is essential that the connection is crafted carefully. This is ORM (Object Relational Mapping) comes in handy as it helps them to access data easily. Usage of an ORM library helps you improve the pace of application development. At the same time, it simplifies and solidifies the implementations.

There are many popular ORM libraries that are used in the .NET world such as Entity Framework, NHibernate, Dapper, LLBLGen Pro, RepoDB, etc. With a multitude of choices available, it's critical that you choose the most efficient but suitable ORM, one of which is NHibernate.

In this post, we are going to introduce NHibernate and how it is different from Entity Framework.

Table of contents

  1. What is NHibernate?
  2. Architecture of NHibernate
  3. Advantages of using NHibernate
  4. Composition of NHibernate Project
  5. Difference between NHibernate and Entity Framework
  6. Conclusion

What is NHibernate?

It is touted as a port of the Hibernate Core for Java to the.NET Framework. It manages the transfer of plain .NET objects to and from a relational database. NHibernate creates SQL for loading and storing objects based on an XML description of your entities and relationships.

Simply put, NHibernate can be considered a database management system. It is -

  • An ORM framework
  • Open-source
  • Designed specifically for use as a persistence layer for the .Net framework based on the Object-Relational Mapping Technique.

It uses code to construct a "virtual representation" of database objects. Impedance mismatch between class and relational databases and tables is solved using this technique.

Architecture of NHibernate

Many systems nowadays are developed with layered architecture; NHibernate is no exception and works flawlessly with it. A layered architecture separates a system into several groups, each of which contains the code that addresses a specific problem area. These groups are referred to as layers. The majority of enterprise-level apps have a three-layered high-level application architecture:

  • The Presentation layer
  • The Business layer
  • The Persistence layer

The above diagram shows a simple NHibernate architecture as well as a high-level view of the NHibernate application. For persistence, the application code uses the NHibernate ISession and IQuery APIs. It only has to manage database transactions, which should be done using the NHibernate ITransaction API.

Advantages of using NHibernate

  • The object is saved in the relational database, where it may be used for all CRUD activities.
  • All database work is handled by NHibernate.
  • Removes the need for SQL statements or the creation of a saved query.
  • All the data access logic may be contained in the application.
  • Our queries will not only be effective but will also be validated by the compiler, thanks to Data Cartography. As a result, if the structure of our underlying table changes, the compiler will notify us that we need to alter our queries!
  • There is no need to write a SQL query for any Create, Retrieve, Update, or Delete (CRUD) operations.

Composition of NHibernate Project

An NHibernate project is composed of multiple elements such as:

  1. The XML mapping files (hbm.xml).
  2. Hibernate Configuration File: hibernate.cfg.
  3. Plain Old CLR Objects (POCOs).
  4. Web.config (An XML File).
  5. Database with Data Access Object (DAO).
  6. Web page / WebForm / MVC: View (Razor).

A simple NHibernate project, on the other hand, is made up of four key components as listed below -

Hibernate Mapping File: To map data to POCOs

  • To instruct NHibernate on how the database should or should not be built.
  • To inform NHibernate what data you want to obtain or store into the database - use data access methods.
  • You'll need a POCO to interface with the data. Although XML mapping files are often used in NHibernate projects, they are not the sole method of mapping data to POCOs.

Hibernate Configuration File: hibernate.cfg

  • Provide details on the database connection configuration, the SQL query dialect, the session factory class, and so on.
  • The "sessionFactory," a thread-safe object created once per application lifetime, is the foundation of NHibernate.
  • A Hibernate mapping file that governs how database tables are mapped to C# objects, based on the configuration. To put it another way - which property corresponds to which database table column.
  • A hibernate mapping XML file.

Plain Old CLR Objects (POCO)

  • In C# or VB.NET, build a class.
  • Only one property can be designated as Virtual in a class (It helps hibernate to create a proxy).
  • It has the option of having a default function Object() { [native code] }.

Main Class / View Pages in aspx:-

  • To work with the Session in order to get and set data into the session so that it may be saved to the database.
  • To use the session to get data from the database and show it on an ASPX page or a VIEW page.

Difference between NHibernate and Entity Framework

The following are some of the differences and advantages of utilizing NHibernate over Entity Framework.

  • Entity framework requires additional connections to handle databases other than MSSQL, which nHibernate provides.
  • In terms of data loading, SQL generation, custom column types, custom collections, and so on, NHibernate can be modified. However, the entity framework has limited extension points.
  • The Entity Framework does not enable lazy loading for scalar attributes like BLOB and CLOB, but nHibernate does.
  • Support for row deletion in a cascade fashion is available only in NHibernate.
  • Preload, Postload, Update, and Delete are also available in NHibernate.
  • Second-level cache support is available in NHibernate.
  • The only negative is - Because of metadata preparation, nHibernate takes longer to start than the Entity Framework.

Conclusion

NHibernate is an open-source object-relational mapper for the.NET framework that is constantly evolving and feature-packed. Thousands of successful projects have employed it and have seen success over the years.

Nieuwste