Tuesday, August 9, 2011

First N-Hibernate Application



Installing NHibernate
Download the NHibernate binaries and extract in a folder. That’s it. Create a project in visual studio and add NHibernate references in it.
Create a business project (class library) . Create the property classes in it as equal number of tables present in database.
Lets start by defining a very simple domain. For the moment it consists of one entity called Product. The product has 3 properties Name, Category and Discontinued.

Add a folder Domain to the FirstSample project of your solution. Add a new class Product.cs to this folder. The code is very simple and uses automatic properties (a feature of the new C# 3.0 compiler)
namespace FirstSolution.Domain
{
   
public class Product
    {
       
public string Name { get; set; }
       
public string Category { get; set; }
       
public bool Discontinued { get; set; }
    }
}

Define the Mapping

Create a folder Mappings in the FirstSample project. Add a new xml-document to this folder and call it Product.hbm.xml. "hbm" part of the file name. This is a convention used by NHibernate to automatically recognize the file as a mapping file. Define "Embedded Resource" as Build Action for this xml file.
In the Windows Explorer locate the nhibernate-mapping.xsd in the src folder of NHibernate and copy it to your SharedLibs folder. We can now use this xml schema definition file when defining our mapping files. VS will then provide intellisense and validation when editing an xml mapping document.
Back in VS add the schema to the Product.hbm.xml file
Let's start now. Each mapping file has to define a root node

xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 

                   assembly="FirstSolution" 

                   namespace="FirstSolution.Domain">

 

  

  

hibernate-mapping>
In a mapping file when referencing a domain class you always have to provide the fully qualified name of the class (e.g. FirstSample.Domain.Product, FirstSample). To make the xml less verbose you can define the assembly name (in which the domain classes are implemented and the namespace of the domain classes in the two attributes assembly and namespace of the root node. It's similar to the using statement in C#.

Now we have to first define a primary key for the product entity. Technically we could take the property Name of the product since this property must be defined and has to be unique. But it is common to use a surrogate key instead. For thus we add a property to our entity and call it Id. We use Guid as the type of the Id but it can as well be an int or a long.
using System;
 
namespace FirstSolution.Domain
{
   
public class Product
    {
       
public Guid Id { get; set; }
       
public string Name { get; set; }
       
public string Category { get; set; }
       
public bool Discontinued { get; set; }
    }
}
The complete mapping file
xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                  
assembly="FirstSolution"
                  
namespace="FirstSolution.Domain">
 
 
<class name="Product">
   
<id name="Id">
     
<generator class="guid" />
   
id>
   
<property name="Name" />
   
<property name="Category" />
   
<property name="Discontinued" />
 
class>
 
hibernate-mapping>