Home arrow PHP arrow Building a Data Access Layer for the Data Mapper Design Pattern

Building a Data Access Layer for the Data Mapper Design Pattern

In this second part of a five-part series I go one step further in the implementation of the Data Mapper design pattern in PHP 5. Specifically, I build a simple MySQL abstraction class which performs a few common tasks. These include connecting to the database server and running hard-coded queries, fetching database rows and so forth.

TABLE OF CONTENTS:
  1. Building a Data Access Layer for the Data Mapper Design Pattern
  2. Review: building a simple domain layer in PHP 5
  3. Building a basic data access layer
  4. Putting the MySQL abstraction class to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
March 17, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Put in a simple way, the Data Mapper design pattern is an elegant solution that allows developers to keep all of the domain objects handled by an application separate from the underlying persistence layer. This characteristic is commonly known as persistence ignorance. As with many other patterns, the implementation of data mappers either in PHP 5 or any other programming language isn’t something that must be done all the time, as this depends heavily on the requirements of the application being developed.

What’s more, in many cases using domain objects that are aware of the data access layer is a suitable approach; the use of Active Record is a good example. However, as soon as the structure of those objects doesn’t match the database schema, the implementation of an intermediate layer that resolves this incompatibility internally is necessary. Here's where data mappers come into play, although there are other patterns, such as Data Access Objects, that also address this issue in an effective manner.

In order to demonstrate the actual functionality of data mappers when using PHP, in the introductory installment of this series I started building a simple domain layer, which was comprised of an abstract class whose interfaced was used for modeling basic domain objects. Essentially, this class was a basic data container that implemented some magic PHP methods for adding and deleting object properties dynamically.

Based upon the interface and functionality encapsulated by this abstract class, it was possible to create concrete user objects easily via simple inheritance. With a domain layer already set, what’s the next step we must take in the implementation of data mappers? Well, as I said before, data mappers behave like "gluing elements" between domain objects and the data access layer. Thus, in this second tutorial I’m going to show you how to implement this layer. For the sake of brevity, it will be made up of a trivial MySQL abstraction class.

Want to learn the full details concerning the implementation of this sample data access layer? Then start reading now!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap

Dev Shed Tutorial Topics: