ORMs WHAT WHY Definition ObjectRelational Mapping ORM is
ORMs WHAT? WHY?
Definition Object-Relational Mapping: (ORM) is a technique (or set of tools) that lets you query and manipulate data from a database using an object-oriented paradigm. When talking about ORM, most people are referring to the tools or framework (i. e. a library) that implements the Object-Relational Mapping technique, hence the phrase "an ORM".
Example (in pseudo-code) You have a book class, you want to retrieve all the books of which the author is "Linus". Manually, you would do something like this: book_list = new List(); sql = "SELECT book FROM library WHERE author = 'Linus'"; data = query(sql); //Something like this. . . Obviously, there’s more code behind it while (row = data. next()) //Now I have a data table, I can iterate on it { book = new Book(); book. set. Author(row. get('author’)); book_list. add(book); }
Example (in ORM pseudo-code) You have a book class, you want to retrieve all the books of which the author is "Linus". Manually, you would do something like this: book_list = Book. Table. query(author="Linus");
ORM Benefits • 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 I 18 N. • It forces you to write MVC code, which, in the end, makes your code a little cleaner. • You have to think of your MODEL (The “M” in MVC) which is how you will represent the data in your code, and map it to the actual DB. Once done, you just refer to the Model (and not the DB) • You don't have to learn to write SQL • Many programmers (web or otherwise) don’t get much training in SQL • SQL can be a very powerful language, but it’s easy to confuse the ‘English-like’ syntax and assume that is all you need to know! • Sanitizing; using prepared statements or transactions are as easy as calling a method. • Basically, the ORM has common code already written for you!
ORM Drawbacks • You have to learn it, and ORM libraries are not lightweight tools; • So, you skip learning DB/ SQL which is complex, and you end up learning the ORM, which can also be complex! • You have to set it up. Same problem. • Performance is OK for usual queries, but a SQL expert will always do better with his own SQL for big projects. • It abstracts the DB. • Good and bad. Not learning how things work behind the scenes makes for a weak programmer!
Where do I get one? An ORM is another package, or 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. So – if you know O-O, you don’t need to learn much more about DBs Some examples: Whichever ORM library you choose, they all use the same principles. There a lot of ORM libraries around here: • Java: Hibernate. • PHP: Propel or Doctrine • Python: the Django ORM or SQLAlchemy • C#: NHibernate or Entity Framework (replacing LINQ)
For this class … This is about as much as we will cover on ORMs You will NOT use an ORM We want you to learn the fundamentals of DBs In industry, you will likely end up encountering ORMs, so awareness is important (and understanding how DBs work, will make it easier to pick up ORM usage)
- Slides: 8