26 March 2007
NHibernate Tutorial, Jumptree Forum Part 3 - Getting Ready
Part I - Why I choose Nhiberneate
Part II - Spring.Net, Setup Nhibernate SupportPart 4 - Setup and Add Category Part 5 - Different Ways of Mapping File Configuration
It has been two weeks since my last two posts in regards to implementing a forum in NHibernate for our Jumptree Website. During these two weeks, I have been trying to read through the NHibernate 1.2 documentation and piece everything together.· From my experience, reading the documentation first is the good way to go. As I searched through if offical Nihbernate discussion board, · by understanding some keywords mentioned in the documentation, it definilty made my research a lot easier.
But first let me share with you my process and help those of you who are interested in starting NHibernate as well to setup your projects.
First let's go over the forum screen shots first


Seems pretty straightforward hopefully. Before we start, let me give you a few quick links to get you prepared because it's a bit hard to find the links on NHibernate website.
Documentation and Downloads
First, this is the link to the NHibernate Documentation
http://www.hibernate.org/22.html#A23
I downloaded the Nhibernate 1.2 documentation in PDF (which I will reference throughout my articles from now on,), here is a direct link
To use Nhibernate 1.2, please download its source which can be found here
Now we have everything, after you downloaded the source code, the structure should look like this

Compile and Generate
Under /src/Nhibernate-src/src/, you should find a Nhibernate.Everything-2.0.sln· (if you don't see anything under /src/Nihernate-src/, you should see a zip file, extract its content right there in the current directory). Open up the Nibhernate.Everyting-2.0 sln, you should see something like the following

Okay, now compile the entire solution and we'll be using the generated libraries later on.
NHibernate Example Analysis
Before we start our Jumptree forum, I want to point out NHibernate comes with a default "Web" example (You can find it under NHibernate.Examples.Web) ·It's a pure ASP.NET website project and not really the typical three tiers that will be implemented for our Jumptree Forum.· However, it's a good starting point for us and we will follow its implementation first and slowly refractor it into our 3 tiers structure as we go along, so just take a note on that as you read and I will reference back to this default web example as we go along.
First, how does the web example work? By looking at the source code, you'll see it placed all of the c# source code into "App_Code". "Domain" folder holds the entity item "Item" which is nothing but a pure "POCO" class that represents an "Item" table in the database. "Persistence" folder holds the data access class which is responsible for operations using NHiberate to list, save, update and delete the "Item"
You might be curious about what "CurrentSessionModule.cs" and "ExampleApplication.cs" are. ·First, you have to understand, Nhibernate's data access depends on "ISessionFactory". The idea is that it's the persistence manager we use to store and retrieve to and from the database. Before a data access operation, you have to "open" up the session and after you are done with your operation, you have to "close" the session.
By reading the Nihibernate PDF documentation on Page 11 – Section 1.4 Play with cat, (Page 11 of 173 in the pdf, not the page number on right bottom side of the footer) , you'll see it talks about
"An ISessionFactory is usually only built once, e.g. at startup inside Application_Start event handler. This also means you should not keep it in an instance variable in your ASP.NET pages, but in some other location. Furthermore, we need some kind of Singleton, so we can access the ISessionFactory easily in application code. The approach shown next solves both problems: configuration and easy access to a ISessionFactory."
Base on this principal, from page 11-12, you'll see the documentation created a dedicated class called "NHibernateHelper". It helps first setting up NHiberneate for usage, then it has GetCurrentSession, CloseSession and what not based on HttpContext. ·
However, the documentation isn't referencing specifically to ASP.NET. So in the web example, you can see it implemented the setup of Nhibernate (which makes sure the application includes all the mapping files and assembly) inside of Application_BeginRequst and End_Request of the HTTP Application Object. ·(That's inside the "ExampleApplication.cs" which is hooked into global.asax) ·and open session and close session inside of "CurrentSessionModule.cs" which is an HttpModule.·
You might be asking what the hell is open session and close session doing inside of an HttpModule? Well, this is actually due to the nature of the web and basically, a session is opened automatically before a web request and close the session at the end of web request. This way, you can perform any data access without worrying anything. It's a "Open Session In View" pattern and you can google it and find out more about it in detail. Just remember, it's something you'll have to do if you want to use it Nhibernate in ASP.Net. It's pretty much a standard now.
Going further, /App_Data holds the mapping file for "Item" class and web.config contains the typical Nhibernate configuration settings.·
Now obviously, the web example isn't using the best practice and it's not a 3 tiers implementation. If you read around, it's not typically done this way, normally, people place the entity classes and their mapping files inside of a separate project and have them as " embedded" resources. So for now, don't worry too much, I'll get into how to properly set up them later on in the process.
Okay, I'm forced to break up this article as it's pretty long now. In the next post, I'll provide information on how exactly I setup Jumptree Forum based on this web example. Stay tuned.
Comment Notification
If you would like to receive an email when updates are made to this post, please register here
Subscribe to this post's comments using
Comment Policy: No HTML allowed. URIs and line breaks are converted automatically. Your e–mail address will not show up on any public page.