OBJECT RELATIONAL MAPPING

Hasan Özdemir
3 min readNov 30, 2021

--

Hello everyone, today we are going to talk about one of the most important technique to manage programming operations and database operations together.

ORM in computer science is a programming technique for converting data between incompatible type systems using object-oriented programming languages. This creates, in effect, a “virtual object database” that can be used from within the programming language.

ORM layer maps tables to classes, rows to objects, and columns to attributes of those objects. Class methods are used to perform table-level operations, and instance methods perform operations on the individual rows.

Object Relational Mapper

Let’s look at example, here is a completely imaginary case with a pseudo language:

You have a product class, you want to retrieve all the products of which the brand is “Iphone”. Manually, you would do something like that:

product_list = new List();
sql = "SELECT product FROM library WHERE brand= 'Iphone'";
data = query(sql); // I over simplify ...
while (row = data.next())
{
product = new Product();
product.setBrand(row.get('brand');
product_list.add(product);
}

With an ORM library, it would look like this:

product_list = ProductTable.query(author="Linus");

The mechanical part is taken care of automatically via the ORM library.

PROS

Productivity ;

  • You write your data model in only one place, and it’s easier to update, maintain, and reuse the code.
  • A lot of stuff is done automatically, from database handling to I18N.
  • It forces you to write MVC code, which, in the end, makes your code a little cleaner.

Application Design ;

  • If you use an ORM to manage the data interface, you do not need to create the perfect database schema in advance. You will be able to change the existing interface easily.

Code Reuse ;

  • One way to reuse data is to create a class library to generate a separate dynamic-link library (DLL). You can create a new application without needing to duplicate the data-access code.

Reduced Testing ;

  • Since the code generated by the ORM is well-tested, you do not need to spend as much time testing the data-access code. Instead, you can focus on testing the business logic and code.

CONS

Getting Started;

  • You need to have deep understanding of ORM. ORM libraries are not lightweight tools.
  • Setting up them with your own programming language and relevant DB

Performance ;

  • High-level abstractions do not always generate the best SQL code, and developers cannot rely on the ORM 100 percent of the time. You still need to know SQL
  • For basic projects performance is acceptable but SQL master allways do better with his own SQL for huge projects.

Technical Issues ;

  • Poor mapping, ORMs can sometimes create an incorrect mapping between data tables and objects. These problems can cause application problems and be difficult to recognize.
  • A poorly-written ORM layer often makes it challenging to improve data schemas. It can often limit your choices and depending on the ORM, your options may be limited. If your ORM doesn’t support migrations, writing the migrations in your OOP is more complicated than writing the code for migrations in SQL

CHALLENGES

A variety of difficulties arise when considering how to match an object system to a relational database. These difficulties are referred to as the object relational impedance mismatch

An alternative to implementing ORM is use of the native procedural languages provided with every major database. These can be called from the client using SQL statements. Data Access Object (DAO) design pattern is used to abstract these statements and offer a lightweight object-oriented interface to the rest of the application

FUN IS PART OF LEARNING

RESOURCES

  1. stackoverflow.com
  2. web.stanford.edu
  3. wikipedia.com
  4. Joel Spolsky

--

--