Few years back I’ve designed an ORM (Object-relational mapping) mechanism as a part of a framework named Pomegranate framework. And now, after a couple of years, I decided to give an introduction to it.
Part one: What is an ORM and why do we need one?
For starters let’s define ORM. Simply put, it’s a couple lines of code which its aim is to store / retrieve data from / to database and your programming language. Most of the time data is stored in form of objects in programming languages and in old times when someone was talking about “databases” he/she mostly would mean RDBMS (relational database manager systems). In a three-tier application architecture design, depicted here , an ORM resides in data layer above database drivers. And of course this is not mandatory and anyone can design their software in anyway they like, but over the years this has proven to be a well thought architecture for a software which needs to make use of a database. All you need to take from this design is that your code’s access to database should be limited to a number of classes and methods called ORM. Wherever you need to store something or retrieve something, all you should be doing is to make a call to a method of your ORM classes, direct access to database is prohibited. In other words, ORM is responsible of taking objects and storing them in database or given a query, takes the results out, turn them into objects and give them back to the caller.
Now why do we need an ORM?
It is one the first lessons learnt in software design that if you are going to write a code multiple times, you should give it a name and make it a function / method. The same philosophy applies here, it is highly probable that you are going to access your data over and over in different parts of your software. And since data can not be used in its raw format (as it is stored on hard disk) it needs to take a new form, and the only data container we have in OOP are objects. Hence, the ORM: some code that translates data between object format and database format!
We will continue this article with “Different types of an ORM” on part two.