ORM (Object Relational Mapper)

I regret that at my first almost two years of software development, I still coded (sometimes copy and paste) my Data Access Layer manually. How many hours that I spent for that? It almost took 30% - 50% of my entire time on my development. Anyway, forget it. I'll show you something cool.

If you are still coding the plumbing Data Access Layer manually like me before, I suggest you to look what ORM is. In short, ORM is the mediator between the relational database and the object oriented application. We know relational is a popular technique in the database world, but in application / our code, usually Object Oriented rules!

So, what we have to do? Usually we build data class based on the table.

Column

Type

StudentId

Nchar(10)

StudentName

Nvarchar(200)

Email

Nvarchar(200)

DateOfBirth

smalldatetime

For example:

We have MsStudent table in the database like this:

 

 

In our application, we try to build our data class. We would map the table in the database to a class, let's call it Student. All the columns in the table are supposed to be properties in that data class. Moreover, we could add some method to the Student class.

public class Student

{

    public Student()

    {        

    }

 

private string _studentId;

 

public string StudentId

{

get { return _studentId; }

set { _studentId = value; }

}

 

private string _studentName;

 

public string StudentName

{

get { return _studentName; }

set { _studentName = value; }

}

 

private string _email;

 

public string Email

{

get { return _email; }

set { _email = value; }

}

 

private DateTime _dateOfBrith;

 

public DateTime DateOfBirth

{

get { return _dateOfBrith; }

set { _dateOfBrith = value; }

}

}

If you have 10, 20, 50 of tables, you have to write it manually. Don't you feel that you are doing something that a machine / computer could do as well? These are plumbing works.

In fact, you generate these codes, based on the table structure using some code generator such as CodeSmith, etc. To do that, you have to build the template first, and then take the table and others properties as parameters, and then the code generator will generate the code for you. Well, but this is not my main point. I'll show you another approach.

ORM comes in to help you doing the mappings. By using ORM, you don't have build the template first. All you have to do is just set-up the configuration to point to the data source you want to map. The ORM tools would map it for you. You'll have the Student class like that as well.

ORM tools / product

There are some ORM tools in the market. Some of them are commercial, some of them are free to use, and some of them even are open source.

Most of them come from third party vendors. But, lately, in .NET Framework 3.5, Microsoft introduces the built-in ORM, it's LINQ to SQL (was DLINQ). Next few months later, you'll see another baby comes up; it's ADO.NET Entities Framework (ADO.NET EDM).

Let's see what they are:

  • Ahah! This is my favorite tools. I have fallen in love with it. In fact, Subsonic offers more than an ORM. SubSonic is a very useful and awesome tool. It is easy to use and configure.

    In brief, SubSonic is open source, and it is created by Rob Conery. For information, Rob has been hired by Microsoft on ASP.NET team.

    I'll post more details about SubSonic on next post.

     

  • In Java world, Hibernate is a very popular and mature ORM. Most of Java developer has used it in development.

    NHibernate is .NET version of Hibernate. NHibernate makes use of XML as the metadata to map between tables in the database and classes in our application.

  • It is the first built-in ORM from Microsoft. You can find it in .NET Framework 3.5. LINQ to SQL is quite easy to configure and use. You can add LINQ to SQL class easily by adding new item, and choose Linq to Sql classes. Its extension is .dbml.

    One big advantage of using LINQ to SQL is we can use LINQ, the built-in query expression in language (C# 3.0 and VB 9.0).

    In contrast, you just only can use SQL Server (2000 and 2008) as the backend database.

    You can find LINQ to SQL posts by Scott Gu here.

     

  • This product haven't released yet. But most people believe that ADO.NET EDM offers higher functionalities than LINQ to SQL does. At least, it supports any data source / database.

    Some people believe that ADO.NET has the ability to reduce / get rid of impedance mismatch. Does it? Let's wait and see.

     

  • See Wikipedia's description.

     

  •  

  • And so many more…

I bet you might have this question in your mind. "Which .NET ORM is the best?"

Well, it depends on many factors and point of view. You can take this discussion for your reference.

Share this post: | | | |
Published Wednesday, May 21, 2008 10:39 AM by Wely
Filed under:

Comments

# re: ORM (Object Relational Mapper)

Wednesday, May 21, 2008 1:25 PM by reyza

bukan mana yang terbaik mungkin, tetapi mana yang sudah digunakan? kalau pertanyaannya seperti itu mungkin yang terjadi adalah share pengalaman :)

saya menggunakan NHibernate...tetapi sekarang menggunakan LINQ to SQL :)

# re: ORM (Object Relational Mapper)

Thursday, May 22, 2008 6:49 AM by De_Joker

pernah NHibernate -> ga efektif untuk development, kenapa ? karena begitu database berubah.... semua kelas berubah

LINQ to SQL... blom pernah :D walaupun uda donlot bukunya Ronald...tp blom sempet baca

Lebih simple make ADO Componentnya .NET da.... coba baca Guidance menggunakan ADO.NET dari p&p.

# re: ORM (Object Relational Mapper)

Thursday, May 22, 2008 9:19 AM by Wely

@reyza: Katanya sih NHibernate cukup mature karna merupakan porting dari Hibernate, salah satu ORM tool yg udah handal. But saya sendiri blm pernah coba.

@De_Joker: Saya pernah baca2 di post juga, katanya emang NHibernate gak terlalu enak dipake (pandangan subjective jg sih)

Saya udah sempat pake Linq To SQL dan SubSonic. Dan In my opinion, SubSonic is better :D

# Subsonic | An Introduction

Friday, May 23, 2008 9:54 AM by Wely

In my previous post , I illustrated about ORM. In this post, I'll show you one of the great ORM tool

# LibraLINQ Documentation (Part 2 – Introduction and Architecture)

Monday, June 23, 2008 11:10 AM by Wely

Introduction LibraLINQ adalah hasil implementasi dari sistem informasi dan web untuk perpustakaan. Dibangun

# re: ORM (Object Relational Mapper)

Tuesday, March 31, 2009 9:14 PM by vignesh

i need how to set datasource in ORM any body can help me .......................

# RealTime - Questions: "How do i get rid of a .netframework error?"

Pingback from  RealTime - Questions: "How do i get rid of a .netframework error?"

# How do i get rid of a .netframework error?

Wednesday, December 1, 2010 7:53 PM by Copious-Systems

Someone referenced this post to answer question "How do i get rid of a .netframework error?"...

Powered by Community Server (Commercial Edition), by Telligent Systems