Click here to Skip to main content
15,884,298 members
Articles / Database Development / MySQL

A Guide to Implementing NHibernate with MySql

Rate me:
Please Sign up or sign in to vote.
4.75/5 (3 votes)
13 Jan 2010CPOL3 min read 14.2K   3  
This is a guide to implementing NHibernate with MySql

My company has recently decided to update the Data Access of our application and I have started looking into ways to design and structure the new Business Logic and data access. Currently, we are looking at using NHibernate, so this is my guide to setting up NHibernate. I have attached a working solution to this post. Please feel free to download it and have a look: Project Link.

Basically, NHibernate is a way of managing information from an application to a relational database. It is part of Hibernate.Core developed for the .NET platform. The way NHibernate works is by mapping the database to the objects with the use of XML files.

The first thing to set up is the Config file. This either goes in the web.config or the app.config:

Config

The config section handles the connection string. This can be set programmatically using the configuration properties when setting up the session. Things to be aware of in the config file are the mapping assembly which will tie up the database with the business objects that are going to be used in the application. The config file also defines the type of database driver class to use. This allows the developer to change the database type to be almost any database in use today. I have used MySql for this example, but NHibernate supports a large number commercially available databases. An important note here is that the MySql.Data.dll library should be in the bin folder (with the NHibernate libraries). Lastly, as I have mentioned is the connection string. This is the same format as any ASP.NET application.

The following code would be useful if you don't want to set up the connection in the config file or only know it at run time:

NhibernateProperties

The next step is defining the business object. This is a fairly self explanatory process and could be automated using code generation tools unless you have a lot of patience. The idea here is to match the fields in the table(s) with the properties you want to be available in the object. I generally suggest using partial classes. The reason for this is that if there is any class logic you want to include in the object and you need to re generate the class for any reason, you won't lose the changes you have made.

Once all the objects have been created, the XML mapping files will need to be created. The format of the XML document should be as follows:

HbmFile

The mapping links the Database table(s) to the object. It does this by specifying the assembly to use as well as the class. Obviously then, the properties are matched with the columns. You can have multiple tables in an object, so you are not restrained to one class per table.

Once the mappings have been done, the last step is to create the data access object. Using NHibernate here is really simple and shows the incredible flexibility and the power of NHibernate.

To access the database, a session object will need to be created. This creates a session for use with the database. It will be stored in the application cache or context if the application is a Web application.

The rest is really easy. To save an object, all you do is call the save method on the session object and pass the object. The same applies for the delete and update. The loading of data is incredibly flexible and by default is lazy loaded. The easiest method of loading data would be getting the object by id, or using the criteria object in NHibernate or using the inbuilt query language. Examples of this are:

SelectMethods

So using NHibernate shows that you are able to add a flexible simple data access solution to your project. I am amazed by the ability to be able to change or add to the database without many detrimental effects on the project. NHibernate makes coding simple and effective. I would love to hear comments from people who have used NHibernate in projects, and how it went.

kick it on DotNetKicks.com

This article was originally posted at http://www.makecodingeasy.com?p=45

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --