Monday, May 31, 2010

NHIbernate Simple Windows App

During Pass few weeks I tried to understand about NHibernate. Really when I start doing to understand about NHibernate actually I have not any idea about what is NHibernate is? What power which it has? What can we do with it? So on. I knew only there is some things called NHibernate . I searched and find out some articles tutorials related to that.
NHibernate is Object Relation Mapping (ORM).
With help of simple Windows Form Application I will show easily implement NHibernate base Application.
First it is very important to find out required DLL. You can download those from.

Create Simple Windows Base Application called NHIbernateTestEx1.
Add below DLL to your reference (Simply write click, brows required DLL and add)

  •  NHibernate.dll
  •  Log4net.dll
  •  NHibernate.ByteCode.LinFu.dll

Figure 1 show after Add required DLL.


In this Example tutorial I will use Local Database. Next add local database to Application. Simple write click on the NHibernateTestEx1 and add new item local database and give name it to EmployeeDB.mdf
Figure 2 show local DB.
Now it is time to add App.config file to project. Simply add App.config file and write as bellow. It will do simply configuration with NHibernate.
NOTE THAT : you must add your Data base connection string on

YOUR DATA BASE CONNCTION STRING GOES HERE


Code for App.config file.







NHibernate.Dialect.MsSql2005Dialect

NHibernate.Connection.DriverConnectionProvider


NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu


Data Source=.\SQLEXPRESS;AttachDbFilename=G:\MYExperiment\NHIbernateTestEx1\NHIbernateTestEx1\App_Data\EmployeeDB.mdf;Integrated Security=True;User Instance=True

false

NHibernate.Driver.SqlClientDriver

ReadCommitted
true





Next create class call Employee.cs (This class is come from the Database table which we created. It can be any class related to database table)
Employee.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace NHIbernateTestEx1
{
class Employee
{
private int _sEmpId;
private string _sEmpName;
private string _sEmpAddress;
private string _sEmpContact;

public int EmpId
{
get { return _sEmpId; }
set { _sEmpId = value; }
}
public string EmpName
{
get { return _sEmpName; }
set { _sEmpName = value; }
}
public string EmpAddress
{
get { return _sEmpAddress; }
set { _sEmpAddress = value; }
}
public string EmpContact
{
get { return _sEmpContact; }
set { _sEmpContact = value; }
}
}
}

Next write the Employee.hbm.xml file for simply mapping process. For that create XML file. And add below code to it.
NOTE THAT: class name should be the class which we created in above step. Employee and it belong to NHIbernateTestEx1 namespace (NHIbernateTestEx1.Employee)
In here id tag is for Primary key of Employee table of Database. And others are the rest of columns.












Simply create form like below.

How to create ISession Factory?
Import bellows DLL.
using NHibernate;
using NHibernate.Cfg;
using System.Reflection;
And write below OpenSesion() method. And it will return ISession.
#region ISession Factory
public ISessionFactory _iSessionFactory;

public ISession OpenSession()
{
if (_iSessionFactory == null)
{
Configuration config = new Configuration();
config.AddAssembly(Assembly.GetCallingAssembly());
_iSessionFactory = config.BuildSessionFactory();
}

return _iSessionFactory.OpenSession();
}
#endregion
Insert Method. In this method get user entered data to text boxes and it will insert into Employee database table with the help of OpenSession() and NHibernate.
#region Insert Data NHIBERNATE
public void InsertEmpData()
{
Employee emp = new Employee();
emp.EmpName = textBox_EmpName.Text.ToString();
emp.EmpAddress = textBoxEmpAddress.Text.ToString();
emp.EmpContact = textBoxEmpContact.Text.ToString();

using(ISession session=OpenSession())
{
using(ITransaction transaction=session.BeginTransaction())
{
session.Save(emp);
transaction.Commit();
}
}
}
#endregion
When Insert Button Click it will call to InsertEmpData() method.
private void buttonInsert_Click(object sender, EventArgs e)
{
InsertEmpData();
}

0 comments:

About This Blog

  © Blogger templates 'Neuronic' by Ourblogtemplates.com 2008

Back to TOP