Fluent Nhibernate, Linq to NHibernate, MS Test, Fluent Assertions, and a working example

So out of pure curiosity I decided to go down the NHibernate path and it had nothing to do with the headache that is Entity Framework… nothing at all.

First I wanted to see where Linq to NHibernate was at after hearing about it years ago. Turns out it’s doing pretty well… and actually works thanks to this link. So with that in mind, I also wanted to check out Fluent NHibernate since I had somehow knew it was a project created with the hope to remove the config files NHibernate used. (No idea how I knew that, must have known someone who used it.) Well just so I could be completely wild, I just decided to try out Fluent Assertions which is a library used to help MS Test assertions to be readable. Crazy idea I know.

Well after a bit of reading and a couple hours of work, I came up with a working example of them all in one project. Go me.

Here it is.

Only thing you need to do is have a database handy and open up the “~\NhibernateTest\NhibernateTest\NHibernateHelper.cs” file and set the Username, Password, Catalogue, and Server name. Then just create a database (But not the tables) that corresponds to the values you entered. Now the nice thing is that the first time you run any of the tests, it (Being Fluent NHibernate) will create that tables and columns needed. This is something that works a lot like Active Records for Ruby. This behavior is controlled by:

.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))

The second True is the important one. If you keep that True, it will drop and recreate the tables every time you run a test. Change it to False and it will stop doing that.

Just keep in mind I’m not presenting any Best Practices here. This is an example but not exactly production ready.