What is an ORM, how does it work, and how should I use one? [closed]
Asked Answered
S

5

228

Someone suggested I use an ORM for a project that I'm designing, but I'm having trouble finding information on what it is or how it works.

Can anyone give me a brief explanation of what an ORM is and how it works and how I should get started using one?

Sammer answered 14/8, 2009 at 19:3 Comment(7)
I found this question but it's from 2009 and I don't know if you already master ORM but look for Entity Framework in CodePlex C#(entityframework.codeplex.com) Here is the source code which you can learn from. Also you can read this. (dbtechnet.org/labs/dae_lab/Orm.pdf)Scud
Just want to point out that ORM cam also relate to Object-Role Modeling en.wikipedia.org/wiki/Object-role_modelingProp
This is a useful question and the votes for the top answer seem to confirm that. A brief description of what ORM is does not require opinion. Just because the user asks for a link to get started the question is flagged off-topic. I feel that this is a frequent over-reaction from moderators.Wept
@CJBS - that in itself is tricky. This is the best I could find on that meta.#275634. Also, this question is now well-answered and available. I don't see the need for more answers, so although I don't like the closed status, I don't really see what re-opening will do for it. I just wish it, and a lot of other questions, weren't closed in the first place. This one was only closed because part of the answer was inappropriate, when the other part was clearly highly beneficial. I don't get it.Wept
@Wept A closed question means the question is off-topic. There could be useful info in the answers, but that does not make the question on-topic. Also, it won't be deleted, it will stay here, but we still need to keep the SO clean and on-topic so it functions at its best. So, to answer your first comment: This is not a useful question, there are useful answers.Phip
@Wept The issue is that the question includes a request for resources (both in the title and in the question). Such requests are off-topic, largely because it results in people just posting a link to their favorite resource, as has happened in several of the answers here. Removing the request for resources from the question would invalidate some existing answers. As regular users, we are not permitted to edit the question to invalidate answers. Thus, it should be closed. If the question was unanswered, then it could be edited to remove those requests and left open.Distinctive
An elected moderator could edit the question to completely remove the resource requests and delete all answers which only respond to the portion of the question asking for resources. For the answers which both answer the main question and provide resources, they should determine if the suggested resources are an integral part of the answer and remove or reword the portion of those which might induce other people to think that resource only answers are OK. Only after all that work is done might this be appropriate to reopen. But, only a moderator has the ability to clean this up to that extent.Distinctive
A
481

Introduction

Object-Relational Mapping (ORM) is a technique that lets you query and manipulate data from a database using an object-oriented paradigm. When talking about ORM, most people are referring to a library that implements the Object-Relational Mapping technique, hence the phrase "an ORM".

An ORM library is a completely ordinary library written in your language of choice that encapsulates the code needed to manipulate the data, so you don't use SQL anymore; you interact directly with an object in the same language you're using.

For example, here is a completely imaginary case with a pseudo language:

You have a book class, you want to retrieve all the books of which the author is "Linus". Manually, you would do something like that:

book_list = new List();
sql = "SELECT book FROM library WHERE author = 'Linus'";
data = query(sql); // I over simplify ...
while (row = data.next())
{
     book = new Book();
     book.setAuthor(row.get('author');
     book_list.add(book);
}

With an ORM library, it would look like this:

book_list = BookTable.query(author="Linus");

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

Pros and Cons

Using ORM saves a lot of time because:

  • DRY: 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.
  • You don't have to write poorly-formed SQL (most Web programmers really suck at it, because SQL is treated like a "sub" language, when in reality it's a very powerful and complex one).
  • Sanitizing; using prepared statements or transactions are as easy as calling a method.

Using an ORM library is more flexible because:

  • It fits in your natural way of coding (it's your language!).
  • It abstracts the DB system, so you can change it whenever you want.
  • The model is weakly bound to the rest of the application, so you can change it or use it anywhere else.
  • It lets you use OOP goodness like data inheritance without a headache.

But ORM can be a pain:

  • You have to learn it, and ORM libraries are not lightweight tools;
  • You have to set it up. Same problem.
  • Performance is OK for usual queries, but a SQL master will always do better with his own SQL for big projects.
  • It abstracts the DB. While it's OK if you know what's happening behind the scene, it's a trap for new programmers that can write very greedy statements, like a heavy hit in a for loop.

How to learn about ORM?

Well, use one. Whichever ORM library you choose, they all use the same principles. There are a lot of ORM libraries around here:

If you want to try an ORM library in Web programming, you'd be better off using an entire framework stack like:

  • Symfony (PHP, using Propel or Doctrine).
  • Django (Python, using a internal ORM).

Do not try to write your own ORM, unless you are trying to learn something. This is a gigantic piece of work, and the old ones took a lot of time and work before they became reliable.

Airlie answered 14/8, 2009 at 19:17 Comment(0)
L
50

ORM stands for "Object to Relational Mapping" where

  • The Object part is the one you use with your programming language ( python in this case )

  • The Relational part is a Relational Database Manager System ( A database that is ) there are other types of databases but the most popular is relational ( you know tables, columns, pk fk etc eg Oracle MySQL, MS-SQL )

  • And finally the Mapping part is where you do a bridge between your objects and your tables.

In applications where you don't use a ORM framework you do this by hand. Using an ORM framework would allow you do reduce the boilerplate needed to create the solution.

So let's say you have this object.

 class Employee:
      def __init__( self, name ): 
          self.__name = name

       def getName( self ):
           return self.__name

       #etc.

and the table

   create table employee(
          name varcar(10),
          -- etc  
    )

Using an ORM framework would allow you to map that object with a db record automagically and write something like:

   emp = Employee("Ryan")

   orm.save( emp )

And have the employee inserted into the DB.

Leroy answered 14/8, 2009 at 21:34 Comment(0)
L
17

An ORM (Object Relational Mapper) is a piece/layer of software that helps map your code Objects to your database.

Some handle more aspects than others...but the purpose is to take some of the weight of the Data Layer off of the developer's shoulders.

Here's a brief clip from Martin Fowler (Data Mapper):

Patterns of Enterprise Application Architecture Data Mappers

Lucais answered 14/8, 2009 at 19:8 Comment(0)
B
4

Like all acronyms it's ambiguous, but I assume they mean object-relational mapper -- a way to cover your eyes and make believe there's no SQL underneath, but rather it's all objects;-). Not really true, of course, and not without problems -- the always colorful Jeff Atwood has described ORM as the Vietnam of CS;-). But, if you know little or no SQL, and have a pretty simple / small-scale problem, they can save you time!-)

Bedspring answered 14/8, 2009 at 19:9 Comment(0)
E
2

Object Model is concerned with the following three concepts Data Abstraction Encapsulation Inheritance The relational model used the basic concept of a relation or table. Object-relational mapping (OR mapping) products integrate object programming language capabilities with relational databases.

Eldreda answered 19/4, 2017 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.