In this post I am going to provide some code to get start with nHibernate framework.
For more information about what is nHibernate and related information, you can refer the following links.
http://community.jboss.org/wiki/NHibernateForNET
http://www.summerofnhibernate.com/
Before starting our implementation, a short introduction about NHibernate.
NHibernate is an ORM (Object Relational Mapper) framework, which is equalent to LINQ to SQL. Normally to access database and do any CRUD (Create, Read, Update, Delete) operations, we will be writing Store Procedures, SQL statements in our projects. By writing such code, we must be care about the syntax of the SQL statements and SQL injection etc.,
But by using ORM, you will be creating a mapping file (that is called as .hbm file in Nhibernate) which specified the table name, corresponding entity class name, table column names with the type of the column and related properties etc., Once the mapping file created, the NHibernate automatically create SQL statement and do CRUD operations by calling related Methods.
So it will reduce the errors which can occur while writing normal SQL statements. This also gives a common framework for working with different databases such as SQL Server, Oracle, mySQL etc., So by changing only some configuration file, we can work with different database easily.
Below is the list of Advantages and Disadvantages using Nhibernate (Note: Points are listed here is my personal views. Incase if someone feels some points are wrong, please comment on this blog. I will verify and update accordingly)
Advantages:
- It is very easy to work with multiple databases using nHibernate. For more information search with “using nhibernate with multiple databases”.
- Once the mapping done with the database Table/View, we can do all the related database operation very easily.
- Can use Caching feature for keeping the first loaded entities in to memory and use it further.
- No need to worry about SQL injection and all. NHibernate will handle automatically.
- If any complicated database job required, can create SQL store procedure and call with nHibernate. No restriction to use only NHibernate provided feature.
- Can dynamically create Queries and execute. It means, there is no need to write any query in a single statement. It can be done thro’ a sequence of code with some conditions.
- To build a query, nHibernate gives various options. They are –
- Can use HQL in CreateQuery
- Can use named queries in GetNamedQuery
- Can use SQL directly in CreateSqlQuery
- Can use the type of object in a CreateCriteria
- As it is very mature and popular, there are multiple project executed with this framework. So we have proof of stability.
- There is a big community for NHibernate. So if any queries can be clarified easily.
- As it is open source, can explore more on how implemented. Can modify for our needs.
Disadvantages:
- Learning curve. As every new technology has this, so not to worry about this.
- Writing mapping XML is very difficult if the database is so big.
- Low performance if designed wrongly.
In this post, I am going to take Customer entity from Northwind database. I also have a screen which shows Customer details. The functionality of the screen would be:
- Initially when page gets loaded, it will show a list of customer in the screen.
- The user can add a new Customer by entering the details in the screen and press Save button.
- The user details can be viewed by clicking Select column in the GridView for a record.
- The user can modify the details and update by Save button.
- The user can delete a particular record by Delete button.
I create a solution with three projects, they are -
- DotNetTwitter.HinHibernate – Project for specifying User interface
- DotNetTwitter.HinHibernate.DataAccess – Project for specifying Data Access methods
- DotNetTwitter.HinHibernate.Entities – Defined Entity classes.
The screen shot of the solution explorer is shown in the figure.
Solution Explorer |
In the UI layer, there is a Default.aspx file and nhibernate.config. The nhibernate.config is the configuration file for specifying the Database connection string, provider and mapping assembly. This file will be accessed from Data Access methods when initializing NHibernate. Below is the nhibernate.config script.
<?xml version="1.0" encoding="utf-8" ?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > <session-factory > <!-- properties --> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string">Data Source=THIRUMALAI-NOTE\SQLEXPRESS;Initial Catalog=Northwind;Persist Security Info=True;Trusted_Connection=Yes;Pooling=yes;connection lifetime=300;</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="show_sql">false</property> <!-- mapping files --> <mapping assembly="DotNetTwitter.HinHibernate.Entities" /> </session-factory> </hibernate-configuration>
The Default.aspx file is used for user interface. For the source code of the aspx file, please verify the attachment located in the next post. The screen shot of the screen would look like below.
Entry Screen for Customer (using NHibernate framework) |
In the entity project, we have Customer.cs Customer.hbm.xml file. The Customer.cs file is the entity class defining the properties for Customer object. The Customer.hbm.xml is the mapping xml file for defining the mapping between the database table and the entity class used in the project.
The Customer.cs class file would be:
public class Customer { public string CustomerID { get; set; } public string CompanyName { get; set; } public string ContactName { get; set; } public string ContactTitle { get; set; } public string Address { get; set; } public string City { get; set; } public string Region { get; set; } public string PostalCode { get; set; } public string Country { get; set; } public string Phone { get; set; } public string Fax { get; set; } }
Customer.hbm.xml
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <class name="DotNetTwitter.HinHibernate.Entities.Customer, DotNetTwitter.HinHibernate.Entities" table="[Customers]" lazy="false"> <id name="CustomerID" column="[CustomerID]" type="String" length="5" > <generator class="assigned"/> </id> <property name="CompanyName" column="[CompanyName]" type="String" length="40" /> <property name="ContactName" column="[ContactName]" type="String" length="30" /> <property name="ContactTitle" column="[ContactTitle]" type="String" length="30" /> <property name="Address" column="[Address]" type="String" length="60" /> <property name="City" column="[City]" type="String" length="15" /> <property name="Region" column="[Region]" type="String" length="15" /> <property name="PostalCode" column="[PostalCode]" type="String" length="10" /> <property name="Country" column="[Country]" type="String" length="15" /> <property name="Phone" column="[Phone]" type="String" length="24" /> <property name="Fax" column="[Fax]" type="String" length="24" /> </class> </hibernate-mapping>Note: Remember to change the Build Action property for this file to Embedded Resource.
Properties file of Customer.hbm.xml file |
The remaining code will be covered in next post.
0 Responses to “Getting Started with NHibernate - Part 1”
Post a Comment