.NET NHibernate tutorial #1 – Project setup

This article is the first of series of articles about using .NET and NHibernate.
In the series we work with tools that can be used in commercial projects freely:
1. Visual Studio 2012 Express for Desktop http://bit.ly/QLJsJT (note that this tool is dedicated to creating desktop applications. If we want to build web-based applications, we need to install Visual Studio 2012 Express for Web)
2. SQL Server 2012 Express http://bit.ly/KJIAMR
3. NHibernate 3.3.3. package http://nhforge.org/

Capture 00. .NET NHibernate logo

Capture 00. .NET NHibernate logo

To prepare the .NET NHibernate environment in Visual Studio 2012 Express we need to create a new project by choosing a project type, for example Console Application, giving a name to our project and indicating the location of our files (Capture 01).

Capture 01. Creating a 'Console Application' project

Capture 01. Creating a ‘Console Application’ project

Now we can see how the created project looks (Capture 02).

Capture 02. Created project

Capture 02. Created project

Then we need a NHibernate package. We can download it from the website: http://nhforge.org/ as a zip package. When we unzip NHibernate, we will see folders and files like below: (Capture 03)

Capture 03. Unzipped NHibernate files

Capture 03. Unzipped NHibernate files

The best practice is to copy all files from the Required_Bins folder to our project. We can make a new folder called NHibernate and copy files there, like bellow: (Capture 04)

Capture 04. Create a new 'NHibernate' folder

Capture 04. Create a new ‘NHibernate’ folder

We can copy all files into our folder, but the critical files are two dlls only: lesi.Colletions.dll and NHibernate.dll. For easier configuration we also need two more files: nhibernate-configuration.xsd and nhibernate-mapping.xsd (see Capture 05).

Files needed for NHibernate

Capture 05. Files needed for NHibernate

Next step is adding NHibernate reference files to our project. To do that, right-click Reference and choose Add Reference (Capture 06).

Capture 06. References

Capture 06. References

As the result, we will see the references group. Then, go to the Browse tab and click Browse... (Capture 07).

Capture 07. Reference Manager

Capture 07. Reference Manager

Next, let us add references from the folder where we copied files. (Like in Capture 08, we can find it in our project folder). When we select both references (lesi.Collection.dll and NHibernate.dll) we click Add.

Capture 08. Select files to reference...

Capture 08. Select files to reference…

Now, we see that references are added and marked (see Capture 09).

Capture 09. Added and marked references

Capture 09. Added and marked references

When we click button OK we will see that references lesi.Collections.dll and NHibernate.dll are added to our project (Capture 10).

Capture 10. New references added to the project

Capture 10. New references added to the project

In case of configuring NHibernate with xml files, we can add schemas to make it easier. For example, in our project we have a file named App.config (that is the xml file). When we open this file we will see a new menu item called XML (Capture 11).

Capture 11. Menu items on opened xml file

Capture 11. Menu items on opened xml file

Next, we click XML and Schemas.... Now, we see all xml schemas that are added or can be added to the project (Capture 12).

Capture 12. XML Schemas

Capture 12. XML Schemas

To add NHibernate schemas to our project we click Add... on the right site of the dialog. It results in opening a window where we can find two xsd files called nhibernate-configuration.xsd and nhibernate-mapping.xsd. We need to select them both and click button Open (Capture 13).

Capture 13. Open XSD Schema

Capture 13. Open XSD Schema

We will see what we have added in the XML Schemas window. As they are marked, we need to click OK only (Capture 14).

Capture 14. New items on XML Schemas

Capture 14. New items on XML Schemas

Now we can start configuring NHibernate in our project.

Download this sample code here.

This code is available on our GitHub repository as well.

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Java by Example App is available at Google Play Store NOW