Introduction to Object Relational Mapping (ORM) in .NET

Filed Under (Development, ORM) by fakhrul on 21-03-2009

Definition

By refering to the wikipedia, the Object Relational Mapping (ORM) is defined as

A programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, a “virtual object database” that can be used from within the programming language. There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to create their own ORM tools.

ORM is place between the database and the .NET object. For example, we have a class name as Student and the table name as TBL_STUDENT. The ORM are used to map the class Student and the TBL_STUDENT. There are 2 way to map the object and the table, one way is using the xml file, and other is using the .NET atrribute. In Nhibernate, I am using the attribute mapping because it is much easier.

 

Why you use it?

  1. You can apply the Domain Driven Design (DDD) easily. 
  2. ORM generate the sql statement for you.
  3. It is price less. Free. There are a lot of open source for ORM.
  4. Support for multiple database. It’s flexible to change which DBMS you want to use. All setting is in the config files.
  5. Good performance. As you know, the ORM frameworks are develop from the expert all over the world. So you don’t worry about it. The only thing you need to worry is, how to use it. (Fortunately, almost all of the API was documented well.)
  6. The source code are provided (for open source ORM). You have the code and you can change it if you are need to do.
  7. Support are available. You can ask a question and discuss anything regarding to the ORM in the forum. 
  8. Faster development process. Forget the learning curve because I think you only need not more than a week to know how to use it.  Once you now, you will be suprise how fast it can be. :D

 

Last word

There are a lot of ORM framework for .NET. For example Spring.Net, NHibernate and  Castle ActiveRecord. I had experience in using NHibernate and Castle ActiveRecord. Castle ActiveRecord is actually sit on top of NHibernate. It was build by a group of geek to simplify the use of NHibernate. 

There are also a lot of tutorial and reference you can get from the internet. You can google it, the keyword is “ORM”, “NHibernate”, “Castle active record”. 

Hope this post had give you an idea about the ORM. Thanks.

 

Other links

http://aspalliance.com/1069_Introducing_Object_Relational_Mapper.1

http://en.wikipedia.org/wiki/Object-relational_mapping

http://www.hibernate.org/343.html

http://www.castleproject.org/ActiveRecord/

Using Castle ActiveRecord and MySql in ASP.NET

Filed Under (Development, ORM) by fakhrul on 07-03-2009

I had started a development of my project using the Asp.Net (Code behind), Castle ActiveRecord and MySql. Here are some of the important points for configuration to make sure it will runs smoothly.

Firstly you need to have all the necessary library:

  1. Castle Active Record
  2. My Sql Connector for .NET

Note: You can put all the assemblies either in the GAC directory or you can create a new folder in your solution folder and puts in there. You can name it as “Libraries”. I suggest you to create a new folder in your solution directory and put every dependency library there. By doing this, you can minimize the chances of missing library.

Secondly, you need to add the reference of

1. Castle Active Record

  • Castle.ActiveRecord.dll
  • Castle.Core.dll
  • Castle.Components.Validator.dll
  • Castle.DynamicProxy.dll
  • NHibernate.dll
  • Iesi.Collections.dll
  • log4net.dll

2. MySql Connector

  • MySql.Data

Then, add the web.config as below


<configuration>

<configsections>
<section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord"/>
</configsections>

<activerecord isWeb="true">
<config>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.MySqlDataDriver"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MySQLDialect"/>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="hibernate.connection.connection_string" value="Database=DB_NAME;Data Source=DB_IP;User Id=DB_LOGIN;Password=DB_PASSWORD"/>
</config>
</activerecord>

</configuration>

Lastly, add the ActiveRecord initialization inside your Global.asax


protected void Application_Start(object sender, EventArgs e)
{
Castle.ActiveRecord.Framework.IConfigurationSource cfgSource = Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler.Instance;

Castle.ActiveRecord.ActiveRecordStarter.Initialize(new[] { System.Reflection.Assembly.Load("Sample.Domain") }, cfgSource);

}