nHibernate (Part2)

Your Ad Here

Introduction:

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.

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.

NOTE:

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.

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.

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

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

<class name="Glasses.Test.User, Glasses" table="Person">

<id name="PersonID" column="PersonID" type="Int32" length="4" unsaved-value="0">

<generator class="identity" />

id>

<property name="Email" column= "Email" type="String" length="50"/>

<property name="Password" type="String" length="50"/>

<property name="Status" type="Boolean" length="1"/>

<property name="DateCreated" type="DateTime"/>

class>

hibernate-mapping>

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 tags contain name which is the name of the property in the User.cs class.
  • column is the "database" column.

Note:

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:

Now let's see the Add method that adds the user object into the database. 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.

public static void Add(User user)

{// make the configuration file

Configuration cfg = new Configuration();

cfg.AddAssembly("Glasses");

ISessionFactory factory = cfg.BuildSessionFactory();

ISession session = factory.OpenSession();

ITransaction transaction = session.BeginTransaction();

session.Save(user);

transaction.Commit();

session.Close();

}

  • 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;

Test.User.Add(user);

}

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 !

Subscribe
Posted in Labels: , kick it on DotNetKicks.com |

0 comments: