Introduction to Fluent Nhibernate

Introduction to Fluent Nhibernate

05 Apr 2024
Beginner
22.8K 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 by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 9th time in a row (2016-2024). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this