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 

Part 1:Why Using Data Access Layer

I know it's too late to talk in this topic, but some times we are using a certain technology or methodology without knowing why we are using it :), we found others using it and we just follow them.
Today I am going to tell you why we are using Data Access Layer (DAL) using an simple application to simplify the idea.



Before we start what is the Data Access Layer:

"A Data Access Layer (DAL) is a layer of a computer program which provides simplified access to data stored in persistent storage of some kind, such as an entity-relational database." Wikipedia

In more simple words, it's an object mapping for your relational database. It means that when you are using DAL you will not access the database directly from code but you will access the DAL which will access the database.

The Example:

Suppose you were asked to design and develop a web application (using any technology) this application will be deployed on 2 servers (Application server & Database Server), the database server will be MS Access or MySQl in phase 1 , then it will be upgraded to MS Sql Server or Oracle according to the increase of data in phase 2.


If you are going to use traditional data access by writing queries inside the code (inline queries) and execute it from your application you will face the following problems:

-Rewriting of common queries ... example if you are going to select from table users with different filters in different files you will rewrite the statement "Select * from users where ...." many and many times

- No Strong Types .. which means that database tables, columns or relations are not presented in your code, so you have to execute queries using text statements with the ability to make mistakes without any compilation errors (you will see errors just in executions) specially if you have change in the database structure or even one column name (you have to go through all the application)

- One database engine support... because all queries are directly written inside the code, so if you decided to change the database engine you have to rewrite many SQL queries because SQL syntax is not standard in all database engines.

-Security Obstacles
.. in our example if the database server does not allow opening ports except HTTP port (port 80), your application will not be able to execute queries or even to connect to the database, but by using DAL you can convert your DAL to a webservice and deploy it on your Database server to consumed from your application. as shown:
I've tried to write in few points why I've used DAL in my last 5 years, in the next part I am going to write about how to choose a suitable DAL with a full comparison between available DAL frameworks and generators.