Using Nhibernate in SharePoint 2010

Running Nhibernate on Sharepoint is not an easy task it might take 1 or 2 days to do it, in this tutorial I will try this task easy by providing a step by step guide starting from how to download NHibernate to how to configure it on sharepoint

You can download the latest Nhibernate version from: http://sourceforge.net/projects/nhibernate/
In my tutorial I am assuming that you know what’s NHibernate and how to use it, if it’s the first time to hear about NHibernate please read the following tutorial first: http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/01/your-first-nhibernate-based-application.aspx

HBM & CS Classes Generators

There are many ways to generate HBM and CS classes
1) MYGeneration Templates: I do not recommend these templates because most of them are not updated to NHibernate newer versions and it’s not easy to change in its templates
Download link: http://www.mygenerationsoftware.com
2) Smart Code Studio: it’s a better way to generate the hbm files and CS classes, I’ve modified in its template to be more standard and simple, you can download my template from this link.
(if you are not using Smart Code Studio you can watch a video on how to use it from this link http://www.kontac.net/site/SmartCode/tabid/55/Default.aspx )
Download link: http://www.kontac.net/site/SmartCode/Download/tabid/64/Default.aspx

Using NHibernate in your sharepoint project:

Create a new class library project in your solution called ABC.Domain and generate the hbm files and cs classes then place into it.

Make sure that all hbm classes are compiled as Embedded resources


Create another class library project called ABC.BL to use the Domain project and to be used by the share point project, don’t forget to add both classes to GAC using gacutil command.

Extract the Nhibernate folder and make sure to add reference to the following DLLs + Adding reference to your Domain project
Remotion.Data.Linq.dll
NHibernate.dll
Nhibernate.ByteCode.Linfu.dll
NHibernate.ByteCode.Castle.dll
log4net.dll
LinFu.DynamicProxy.dll
Iesi.Collections.dll
Castle.DynamicProxy2.dll
Castle.Core.dll
Antlr3.Runtime.dll



After adding the references to your project make sure to install them to GAC by executing the following commands on Visual Studio Command Prompt
gacutil -i "\Remotion.Data.Linq.dll"
gacutil -i "\NHibernate.dll"
gacutil -i "\Nhibernate.ByteCode.Linfu.dll"
gacutil -i "\NHibernate.ByteCode.Castle.dll"
gacutil -i "\log4net.dll"
gacutil -i "\LinFu.DynamicProxy.dll"
gacutil -i "\Iesi.Collections.dll"
gacutil -i "\Castle.DynamicProxy2.dll"
gacutil -i "\Castle.Core.dll"
gacutil -i "\Antlr3.Runtime.dll"
gacutil -i "\ABC.Domain\Bin\Debug\ABC.Domain.dll"

Configure your Web.config & hibernate.cfg.xml

Prepare you hibernate.cfg.xml and place it inot your website bin folder (eg. C:\inetpub\wwwroot\wss\VirtualDirectories\80\bin)

Now All references and configurations are ready, but if you tried to deploy your solution you will get a run time error “Could not load file or assembly 'xyz' or one of its dependencies”, Now you will just need to add qualifyAssembly tags to your web.config (eg C:\inetpub\wwwroot\wss\VirtualDirectories\80\ ) to allow Nhibernate to load assemblies form GAC and not to expect it in the bin folder.

To use Nhibernate inside your sharepoint project you can refer to this tutorial http://www.codegod.de/WebAppCodeGod/nhibernate-tutorial-1---and-aspnet-AID25.aspx

Examples provided by: Enozom Software Development Company