We all learn things our own way. This post is about the road i took to learn what a data layer is, i hope it is helpfull to beginners.
Step 1: The first code
I started programming in PHP and used mysql as the database. The code looked like the code in figure 1. It works but has downsides. I would just repeat the code from figure 1 over and over again wherever it was needed. What if the database password name or the user changed? Well there is allways find and replace right ;)
Step 2: Functions
Next i learnt about functions and how they can be used to get rid of repeated code. Functions will be put in a separte file that will be included. This makes the main code easier to read. Note that the db_insert function generates the query automatically, this prevents mistakes and saves time. This function does not prevent SQL injection but it is easy to build it in now because it's a function.
Step 3: Classes
Next i learned about classes. Classes open up alot of possibilities and approaches. With classes you can build a real reusable layer of related classes where each class has it's own responsibility. In figure 3 the SelectQry class lets you build a query object that you can configure by using its methods. With functions that is not possible unless you use globals or have a really long argument list.
Step 4: ORMing, patterns, and libraries
Once you learn OO programming you dont want to deal with records in the form of plain arrays annymore. Instead you want to make classes for each thing you want to store, like a User class or Post class. Then we want to create objects and just save and retrieve them on demand.
So we want objects in memory and database records on the disk, the data needs to be transferred between these two different places. Bridging this gap is called object relational mapping.
An ORM can be implemented in different ways and patterns exist. Two patterns that come to mind are the Data Mapper pattern and the Active Record pattern described by Martin Fowler. Building a simple ORM is possible but there are tons of good libraries available in all languages.
Step 5: ???
There are some recent developments in the land of databases which i dont know much about. The relational database with tables, rows and columns is around for 30 years now and new systems are here to address new problems. This site titled “Is the Relational Database Doomed?” looks like a good starting point to me for further research.
- http://kohanaframework.org/3.0/guide/orm [Kohana's ORM - PHP] I used Kohana's ORM to build this site. It uses the active record pattern.
- http://www.doctrine-project.org/projects/orm.html [Doctrine ORM - PHP] A verry powerful ORM wich i like alot. Doctrine 1 was active record like, Doctrine 2 uses the datamapper pattern.
- http://wiki.eclipse.org/EclipseLink/UserGuide/JPA [Eclipse link ORM - Java] Eclipse link is the reference implementation of the JPA. When using it we had alot of wierd errors popping up here and there.
- http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software List of ORMs.