Introduction to Fluent Nhibernate

24 Aug 2022

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

Learn to Crack Your Technical Interview

Accept cookies & close this