Okay, so I have been guilty in the past for not implementing any form of ORM (Object Relational Model) for some of the projects I have written. The main reason for this is/was that the projects I have worked on have never really used them. But the more I code the more I realise I sometimes find myself re-coding the same stuff. I mean my understanding of an ORM tool is that it creates a set or sets of classes that map to one or more databases. But why is this beneficial and why does this improve my life as a developer. I started out by reading this article on Wikipedia, which is not as detailed as I had hoped, but I guess its a starting point, because it lead me to this article. I also found this article and watched the video and from it got to understand that for the relational world to co-exist with the OO world several things need to be considered:
- Inheritance: In the OO world a User object might serve as a base class for users of type teacher or student. How do you map this relationship in the relational database?
- Granularity: This happens if the classes do not match up to the tables in the database. You might for example have a situation where a User is associated to a country. In your database you might represent this as two tables, but in your class you might represent the country as a property or field of the User class. So there exists a mismatch. How do you map this situation?
- Identity and Equality: Lets say you query a database for a User with ID 100. The result you get back will always point to user with ID 100. If you look at it from an OO perspective, will a User with ID point to the same object, or multiple instances of it?
- Association (directionality): In an OO model you associate classes with each other by placing references in each class of the other. In a relational model you create the associations by placing a foreign key in a table.
- Type systems: The types in a database are quite different from those in an OO environment. For instance a VARCHAR with a size of 100 is not the same as a string, because a string is dependant on the amount of memory available.
I then read two StackOverflow topics, one here and one here. From what I could gather ORM’s are not great at performing bulk operations, but what they lack in that area they make up for in developer productivity.
I remember coming across an article that compared the performance between various ORM mappers on this website.