Monday, March 07, 2011

nhibernate 3.0 tutorial with fluent nhibernate and linq 2 nhibernate

This is a quickstart tutorial for getting up and running with NHibernate 3.0 using Linq to NHibernate and Fluent NHibernate for configuration.

You're going to need:

VS2010 - Express edition will do. (we're going to be writing a Console Application)
SQL Server 2008 - Express edition should do
NHibernate 3.0 download this tutorial was written with NHibernate 3.0.0
Fluent NHibernate Stable PreRelease binary v1.x build#694
step 1 - create a console project & extract the dlls from the zip files
I've called it SimpleNHibernateClient on my machine. Then extract the .dlls from NHibernate-3.0.0.GA-bin.zip\Required_Bins into your lib folder.
Then also extract the .dlls from the fluentnhibernate-NH3.0-binary-1.2.0.694.zip file also into your lib folder. You'll probably find that the fluent zip already contains the required NHibernate .dlls don't worry it should be fine.

step 2 - add the references to the relevant .dlls
Your references should look something like this:
step 3 - create some entity classes
Next create some entity classes in your project. Now be sure here to make the properties public virtual NHibernate needs the entities properties to be virtual so it can work its magic and pull the information from the database at runtime.
Your code should look something like this:

using System;
using System.Collections.Generic;
namespace SimpleNHibernateClient.ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello NHibernate");
Console.ReadLine();
}
}
public class Car
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
public virtual string Description { get; set; }
public virtual Make Make { get; set; }
public virtual Model Model { get; set; }
}
public class Make
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList Models { get; set; }
}
public class Model
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Make Make { get; set; }
}
}

Now we've got all your entities in the same file as your actual program. Obviously this isn't best practice, so it'd be good if you extracted them out to their own files.

step 4 - create an empty database
I did this though Server Explorer and just created an empty Sql2008 Express database.

step 5 - create the database mappings
Ok, so now you have to tell NHibernate how to map your entities to the database. We're going to use the Fluent NHibernate interface to do this, so all configuration is in code. There are other ways of doing this. You could use xml configuration file .hbm (Hibernate Mapping files) or you could even use attributes.
We're going to use Fluent because it lets us keep our entity POCO's really clean and simple.

Create another class such as CarMap in my example and inherit for ClassMap<Car> this is what lets Fluent NHibernate know to use these mappings with the Car class you've just defined. If you look at the configuration,  it's saying what's the Id field. Handily called Id in this example. Also note the real magic References(x=>x.Make).Column("MakeId") here we're telling NHibernate that a Car has a Make and it's realted by the MakeId column. NHibernate will generate a default column name if you want, I'm doing it for affect. We also specify the table name Table("Car") we don't really need to do this, but we've got that extra little bit of control if we want.