
Object-Relational Mapping (ORM) for Efficient Data Management
Discover the concept of Object-Relational Mapping (ORM) and how it simplifies querying and manipulating data from databases using an object-oriented approach. Explore ORM benefits like DRY principle, MVC code structure, and the elimination of direct SQL queries to enhance data model management.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
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.setAuthor(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 = BookTable.query(author="Linus");
LINQ Where clause usage: // Student collection IList<Student> studentList= new List<Student>() { new Student() { StudentID= 1, StudentName = "John", Age = 13} , public static void Main() { var filteredResult = from s in studentList where isTeenAger(s) select s; } new Student() { StudentID= 2, StudentName = "Moin", Age = 21 } , new Student() { StudentID= 3, StudentName = "Bill", Age = 18 } , new Student() { StudentID= 4, StudentName = "Ram" , Age = 20} , new Student() { StudentID= 5, StudentName = "Ron" , Age = 15 } }; // LINQ Query Syntax to find out teenager students public static bool IsTeenAger(Student student) { return student.Age > 12 && student.Age < 20; } var teenAgerStudent= from s in studentList where s.Age > 12 && s.Age < 20 select s;
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 I18N. 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 are 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)