Skip to content
tezvyn:
Menu

ORM: The Virtual Object Database Layer

Source: Wikipedia: Object-relational mappingMediumHow cards are made

ORM converts data between relational databases and object-oriented program memory, creating a virtual object database inside your code. The footgun is designing object models that ignore the relational structure, forcing awkward translations you never see.

Why it exists

Relational databases store data in a structured format. Object-oriented programming languages store data as objects in memory. These two models are incompatible. Without a conversion technique, a program must manually move data between the database and memory. Object-relational mapping exists to automate this conversion, creating a virtual object database that can be used from within the program.

The mental model

Think of ORM as a translation layer between two worlds. On one side is the relational database. On the other is the memory of an object-oriented programming language. The ORM creates a virtual object database that your program interacts with directly. It handles the conversion so your code can work with memory objects instead of the raw data stored relationally.

How it works

The technique converts data between the relational database and program memory. When data moves from the database into the program, the ORM transforms it into objects in the memory of the object-oriented language. When the program modifies those memory objects, the ORM converts the changes back into a form the relational database can store. This bidirectional conversion creates the illusion of a native object database inside the program.

When to use it

Use this technique when you want to access a relational database through the object-oriented paradigm. It is appropriate when you prefer to reason about data as objects in memory rather than as raw relational data. It is also useful when you want the program to treat the database as a virtual object database that can be used from within the program.

When not to use it

Avoid this technique when the abstraction adds more cost than value. If your problem is naturally relational and does not benefit from an object representation, the conversion step is unnecessary overhead. It is also a poor fit when you need explicit control over the conversion between the database and memory, because the virtual object database hides those details from you.

One canonical example

Consider a program that reads data from a relational database. Without the technique, the program must manually convert the database data into objects in memory. With an ORM, the program requests data through the virtual object database, and the technique automatically converts the relational data into objects in the memory of the object-oriented programming language. When the program changes those objects, the ORM converts the memory changes back for the relational database.

Interview question

What footgun does the card identify as the main danger when using an ORM?

  • a.The ORM introduces memory overhead that slows down all database transactions
  • b.Object-oriented languages are fundamentally incompatible with relational databases without an ORM
  • c.Object models are designed without regard for relational structure, forcing awkward unseen translationsCorrect
  • d.Changes made to memory objects are never propagated back to the relational database
Why?

The card explicitly identifies the danger as object models that ignore relational structure, which forces awkward hidden translations. Option B is wrong because the card states that manual conversion is possible without an ORM, so the languages are not fundamentally incompatible with it.

Just read this? Test yourself on what you have been reading.

Read the original → en.wikipedia.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles