Tuesday, February 12, 2013

Fluent NHibernate

I've recently finished reading the book Test Driven Development with C#.  I would recommend this book to anyone interested in unit testing, integration testing and end-to-end testing.  If you're attempting to enter into the complex world of test driven development, this book is excellent and presenting examples that work and make it all clear.  One of the packages used in the integration testing section is called NHibernate, specifically a version called Fluent NHibernate.  This package takes the place of a database engine and allows you to mock a few tables with values in them avoiding the need to maintain a real database with test data.  If you're looking for more information on this package, I would recommend this wiki page for starters:

Fluent NHibernate, Getting Started

You'll have to startup the Tools -> Library Package Manager -> Package Manager Console.  Then use this command to install Fluent NHibernate:

PM> Install-Package FluentNHibernate

Now go back to Tools -> Library Package Manager -> Manage NuGet Packages for Solution and "Manage" Fluent NHibernate.  Make sure there is a check next to the unit test project you want to use this package for:



In order to use Fluent NHibernate, you'll need to also add NHibernate.  I used the search in the NuGet package manager.

Don't forget to add your usings:

using NHibernate;
using NHibernate.Mapping;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using FluentNHibernate;
using FluentNHibernate.Mapping;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Cfg;

I had to dig through the sample code to get the proper usings to work with the code fragments that were provided on the wiki page.  I hope the above list saves you some time and frustration.

I'll revisit this subject after I've become more familiar with it's use.  So far, from what I've played with, it provides a capability that I've been wanting for years.