Introducción a Los Object Relational Mappers (ORM)

What is a Object Relational Mapper?

A simple answer is that you wrap your tables or stored procedures in classes in your programming language, so that instead of writing SQL statements to interact with your database, you use methods and properties of objects.

In other words, instead of something like this:

  String sql = "SELECT ... FROM persons WHERE id = 10"
  DbCommand cmd = new DbCommand(connection, sql);
  Result res = cmd.Execute();
  String name = res[0]["FIRST_NAME"];
you do something like this:

  Person p = Person.Get(10);

or similar code (lots of variations here). The framework is what makes this code possible.

Now, benefits:

  1. First of all, you hide the SQL away from your logic code
  2. This has the benefit of allowing you to more easily support more database engines
  3. For instance, MS SQL Server and Oracle have different names on typical functions, and different ways to do calculations with dates. This difference can be put away from your logic code.
  4. Additionally, you can focus on writing the logic, instead of getting all the SQL right.
  5. The code will typically be more readable as well, since it doesn't contain all the plumbing necessary to talk to the database.

Casiano Rodríguez León
2015-01-25