Introduction to Fluent Nhibernate

Introduction to Fluent Nhibernate

05 Apr 2024
Beginner
23K Views
2 min read
Learn with an interactive course and practical hands-on labs

Self-Paced ASP.NET Core Course

Fluent Nhibernate is a great O/RM tool to specify strongly typed mapping in the form of classes on the top of NHibernate framework. Fluent Nhibernate offers a simplified implementation of Nhibernate without using the XML(hbm) mapping files. Fluent NHibernate lets you write mappings in strongly typed C# code. Also, you can learn our previous article about entity framework

Fluent nhibernate also provides the option for you to do an automapping where the mapping is automaticaly created and managed by fluent nhibernate. Creating Fluent Nhibernate automapping is pretty much straight forward in OneToOne, OneTomany relationship, but when it comes to ManyToMany relationship it is not easy.

Nhibernate Configuration files issues

The most important complains I personally have on NHibernate configuration files are:

  1. It is error prone.

  2. Errors in configuration file can be hard to trace.

  3. It is not refactoring friendly.

Fluent NHibernate Tools

  1. Auto mapping

    Fluent NHibernate automatically mapping all your entities based on a set of conventions.By using the auto mappings, you can map your entire domain with very little code, and certainly no XML.

  2. Persistence specification testing

    It provides a round-trip testing for your entities, without ever having to write a line of code. You can quickly test your mappings using the PersistenceSpecification class.

  3. Fluent configuration

    Fluent NHibernate provides an API to configure NHibernate for use with your application

  4. Database configuration

    Fluent NHibernate provides an API for configuring your database, this can be used in combination with the fluent configuration or on it's own.

Installing Fluent NHibernate

Fluent NHibernate is an open source project (OSS). Thus, the code and/or the binaries are freely available and can be downloaded from Github. Before you can start coding, you must first download Fluent NHibernate libraries that can be found here: http://fluentnhibernate.org/ .Unzip downloaded file and extract it into a folder of your choice.

Now start Visual Studio and create new web application project. Now you need to add references of Fluent NHibernate dll to your project. Right click on the References in your solution explorer and select Add Reference. Now browse the directory where you have extracted your Fluent NHibernate libraries and reference the following dll files:

  1. FluentNhibernate.dll

  2. NHibernate.dll

  3. NHibernate.ByteCode.Castle

Now you can continue to work on your web application with fluent nhibernate mapping to the database using strongly typed C# classes.

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this