NHibernate .NET Framework ORM

NHibernateNHibernate is the most common ORM (Object-Relational Mapping) that used on .NET Framework Programming. With NHibernate the relational object such as Database Tables can be represented with Object such as Classes on Object Oriented Programming. So how to start it?

First step, of course you must download the NHibernate library. Where? Just go to http://sourceforge.net/projects/nhibernate/files/ and there is many files but the most important is bin package. Extract the zip compressed package to some folder.

Second step, adding the library or references to Visual Studio Project. Open your Visual Studio 2008 and create new project. Just right click on the References then Add References, Browse the .dll files named NHibernate.dll on Required_Bins. Then Add the LazyLoading references, i’m using LinFu so add the NHibernate.ByteCode.LinFu.dll

Then next step is preparing the database. Let’s see now i’m using MySQL as the DBMS you also can use other DBMS that recommended by NHibernate (look at Dialect that supported by NHibernate at reference package). Suppose there are MySQL Database named “nhibernate” with username and password = “nhibernate”

Then let’s create the app.config for connection between NHibernate Framework and MySQL DBMS. To create app.config just right click on your project then “Add New Item” and choose Application Configuration File.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.connection_string">server=127.0.0.1;User Id=nhibernate;password=nhibernate;Persist Security Info=True;database=nhibernate</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
      <mapping assembly="SimpleNHibernate"/>
    </session-factory>
  </hibernate-configuration>
</configuration>

For helping you creating the App.config file, you can set the xml schema. Click on editor window where App.config file opened and look at properties window and set the Schemas, browse to “nhibernate-configuration.xsd” at Required_Bins folder from NHibernate bin package.

Some explanation :

  1. Dialect is what DBMS that you’re using, look at the references for the list of Dialects
  2. Connection String can be got from ADO.NET Connection string, it’s same
  3. Proxy Factory is the LazyLoading library that you choose
  4. mapping is for map the relational table to object class on .NET usually use the Project Name

Leave a Reply

Your email address will not be published. Required fields are marked *

Post Navigation