NHibernate Part II by azamsharp


I know you all must be really anxious to find out that how NHibernate works and how it can make your life easier.In this article we will look at the add feature of the NHibernate. I will go step by step so you understand what is going on.In future articles we will implement an interface and inherit from that interface to get the common functionality. But for now let’s look at the easy way to add the object.

Making a Class Library:

The first task is to create a class library which will be mapped using the NHibernate. So, lets make a simple class User.

public class User{
	private int personID; 
	private string email; 
	private string password; 
	private bool status; 
	private DateTime dateCreated; 
	// Defining properties 
	public int PersonID 
		get { return personID; }
		set { personID = value; }
	public string Email 
		get { return email; }
		set { email = value; }
	public string Password 
		get { return password; } 
		set { password = value; }
	public bool Status 
		get { return status; } 
		set { status = value; } 

	public DateTime DateCreated 
		get { return dateCreated; }
		set { dateCreated = value; }

As you can see that there is nothing special about the class. It has some private variables which you can access using the properties. All of this has been explained in my articles before.

All these properties will map to the database fields. That’s why I have created the database script file for you. Just run the script file using query analyzer and it will create table for you. If it does not work than you can easily create a table based on the properties above. This file is available along with the Attachment Links below.

Creating Mapping File:

Now let’s take a look at the mapping file for the User.cs class. This mapping file is called (User.hbm.xml). It’s a good idea to name the mapping file based on the class name.

<classname="Glasses.Test.User, Glasses"table="Person">

Let’s take a look at the mapping file now.

  • Glasses.Test.User is the name of the class that I am using. Glasses is the name of the assembly, Test is the name of the folder and finally user is the class.
  • Person is the name of the table
  • identity means that PersonID is the identity (Automatically generated) column in the database and we don’t have to supply it.
  • The <property> tags contain name which is the name of the property in the User.cs class.
  • column is the “database” column.

Make sure that the mapping file is configured as the “Embedded Resource”. Right click on the “User.hbm.xml” file and select properties and set build option to “Embedded Resource”.

Add Method Code:

public static void Add(User user) {// make the configuration file

Now let’s see the Add method that adds the user object into the database.

Configuration cfg = new Configuration(); 
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();
  • First we are making a Configuration object cfg
  • We load the assembly using the AddAssembly method
  • ISessionFactory interface creates a session factory which can be used to create sessions.
  • ISession interface is used to create independent session which is opened by using the factory.OpenSession() method
  • Once the session has been open than it means that you can communicate with the database.
  • ITransaction interface which begines the session transactions.
  • session.Save(user) is used to save the session.
  • transaction.Commit() is used to save the data to the database.
  • session.Close() finally closes the session.

The Client Code:

The Client code is pretty simple and self explanatory.

private void Button1_Click(object sender, System.EventArgs e){
User user = new User(); 
user.Email = "codersource@source.net"; 
user.Password = "mypassword"; 
user.Status = true; 

user.DateCreated = DateTime.Now;

Don’t wait now and check your database. A new entry should be added if you have done everything right. Isn’t this the coolest thing ever. In the upcoming articles we will see more features of NHibernate.

I hope you enjoyed the article. Happy programming !