Someone suggested I use an ORM for a project 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 or a link as to where I can learn more about it?
This is a huge topic. Pick up a good hibernate book and it should explain ORM in detail before getting to the nitty gritty hibernate material.
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
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!-)
Quick intro
An ORM (Object-Relational Mapping) is a tool that let you query and manipulate data from a database using an object paradigm.
It's a completely ordinary library written in your language that encapsulates the code needed to manipulate the data, so you don't use SQL anymore, but directly an object of you language.
E.G, 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, it would look like that :
book_list = BookTable.query(author="Linus");
The mechanical part is taken care automatically by the ORM.
Pros and cons
Using an ORM save a lot of time because :
- DRY : You write your data model in only one place, it's easier to update, maintain and reuse the code.
- A lot of stuff is done automatically, from the database handling to I18N.
- It forces you to write MVC code, and in the end your app is 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 whereas it's a very powerful and complex one)
- Sanitizing, using prepared statements or transactions are as easy as calling a method.
Using an ORM 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 binded to the rest of the app, so you can change it or use it anywhere else.
- It let you use OOP goodness like data inheritance without head ache.
But ORM can be a pain :
- You have to learn it, and they are not lightweight tools;
- You have to set it up. Same problem.
- Performances are ok for usual queries, but a SQL master will always do better with his little hands for the big dirty works.
- It abstracts the DB. While it's ok if you know what's happening behind the scene, it's a trap for the noobs that can write very greedy statements, like a heavy hit in a for loop...
How to learn about them ?
Well, use one. What ever the one you choose, they all use the same principles. There are a lot of ORM around here :
- Java : Hibernate.
- PHP : Propel or Doctrine (I prefer the last one).
- Python : the Django ORM or SQLAlchemy (The last one is my favorite ORM ever).
If you want to try an ORM in Web programming, you'd better use directly an entire framework stack like :
Do not try to write you own ORM, unless to learn something. This is a gigantic piece of work, and the old ones took a lot of time before becoming reliable.
The first chapter of the Hibernate book Java Persistence with Hibernate (3rd ed.) has an excellent overview of general ORM concepts and discusses motivation and design of ORMs. Highly recommended, even if you don't work with Java.
Can anyone give me a brief explanation...
Sure.
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.
Oops it was tno that brief but I hope it is simply enough to catch other articles you red
DALMP http://code.google.com/p/dalmp/ could be a good one for php/mysql currently supporting many caches backends like redis/memcache/apc