Getting Started with Fluent NHibernate RTM and ASP.NET Part I

As you know Fluent NHibernate is a Framwork which works on top of NHibernate; giving you the option of working with less xml configuration files. As well the advantage of mapping tables in a more natural way as you will see.

In this first part of the tutorial, I will will try to show you how powerful and easy is to use Fluent NHibernate RTM in an ASP.NET application , we will be working in a real life application using NorthWind Data Base from Microsoft Sql Server 2005 (you can use any if you want).

Scope:

  • How to Map Object to Tables following Fluent NHibernate Notation
  • How to Display Data and Images into a GridView

Technical Environment:

MS Sql Server 200x, Visual Studio 200x and .NET 3.0 Framework.

Ingredients:

The first thing you have to do, is to create a new website project in Visual Studio; once you are done with it, do right click over the project and select “Add References” from the menu.

After that, choose the “Browse” tab, and look for your Fluent NHibernate location, from there you have to copy 3 different dll’s:

  1. NHibernate.dll: Main dll to Load The NHibernate Framework.
  2. FluentNHibernate.dll: dll which loads the logic for your C# mapping.
  3. NHibernate.ByteCode.Castle.dll: Proxy to instantiate your classes at runtime.

After that, your configuration is ready to go, and is time to setup your connection.

lets proceed with our Data Base, you have to install the sample “North Wind” Data Base, by default the location path is “C:\SQL Server 2000 Sample Databases”

now lets do Some Mappings:

Hint: if you are running a web application or your cs code should be put into the App_Code folder of your application.

and in it, you can create two separates folders “Entities” and “Mappings”.

lets map the Categories table

you have to know which type of data are you mapping to; due to no all C# CLR map to SQL Types:

the C# class for categories will be like this:

public class Categories

{

public virtual int CategoryID {get; private set;}

public virtual string CategoryName {get; set;}

public virtual string Description {get; set;}

public virtual byte[] Picture { get; set; }

}

all the properties are virtual due to the lazy proxy have to instantiate some objects of this type.

the CategoryID “set” has been declared private due to is an primary key in our database, so we don’t want it to be modified.

The image field is mapping to a byte[] (array of bytes in C#).

Using NHibernate the categories.hbm.xml file to the table will be:

<hibernate-mapping xmlns=”urn:nhibernate-mapping-2.2default-access=”property auto-import=”truedefault-cascade=”none default-lazy=”true“><class xmlns=”urn:nhibernate-mapping-2.2name=”Categories, App_Code.8bmzcbh2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=nulltable=”`Categories`”>

 

<id name=”CategoryID“>

<column name=”CategoryID” />

<generator class=”identity />

</id>

<property name=”CategoryName“>

<column name=”CategoryName” />

</property>

<property name=”Description” >

<column name=”Description” />

</property>

<property name=”Picture” >

<column name=”Picture” />

</property>

</class>

</hibernate-mapping>

due to there is no dtd for this xml, we always are prone to errors all the time.

now let go and do the mapping using Fluent HNibernate.

you have to create a .cs file which has to have the notation ClassName+Map.cs for this case, it will be CategoriesMap.cs

you have to add the “using FluentNHibernate.Mapping;” import.

public class CategoriesMap:ClassMap<Categories>

{

public CategoriesMap()

{

Id(x => x.CategoryID); //Id is mapping to the <id> tag;

Map(x => x.CategoryName); //Map is mapping to the <property> tag

Map(x => x.Description);

Map(x => x.Picture);

}

}

All the classes which are mapping have to inherit from “ClassMap<>” and have to be of the type of the mapping for this case “Categories“.

at run time this class will generate the hbm.xml file for Categories.

all first mapping is already bound so we can proceed with the connection, you can create it in any where but you have to keep in mine the imports which you have to do:

using FluentNHibernate.Cfg;

using FluentNHibernate.Cfg.Db;

using FluentNHibernate.Automapping;

using NHibernate;

using NHibernate.Cfg;

using NHibernate.Tool.hbm2ddl;

you can create an user or connect to the DataBase using the default connection, for this particular case we will be using a created user:

Hint:if you want to connect with another user don’t forget to chance to mixed authentication mode your MsSqlServer Connection.

This is the way how you will create your sessionFactory:

public static ISessionFactory CreateSessionFactory(){

ISessionFactory sessionFactory =

Fluently.Configure().Database(MsSqlConfiguration.MsSql2005

//if you are using another MsSqlVersion you just have to change the number (2000,2008)

.ConnectionString(m => m.Server(@”.\SQLEXPRESS”)

// you don’t have to use “.\” in the connection string if you are running a full MsSql Server Version

.Username(“DBUser”)

.Password(“passuser”)

.Database(“Northwind”)))

.Mappings(m =>

m.FluentMappings.AddFromAssemblyOf<yourAssemblyfileName>())

.BuildSessionFactory();

return sessionFactory;

}

Hint:

yourAssemblyfileName“, if you are running a webApp you can put here any of the classes which you are using for instance Categories will be fine, if you are running a console or windows application you can put the entry point or the class which have a main method, by default it is Program.

Now we are ready to birng out data into the front end using ASP.NET and the GridView component.

Go to:

Getting Started with Fluent NHibernate RTM and ASP.NET Part II

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.