mercredi 24 juin 2015

What problems can my DAO pattern have, when compared to DataMapper?

Suppose I have a Product class that loads a Motor.

class Product
{
   function productLoadMotor()
   {
       $this->motor = new Motor();
       $this->motor->loadMotorData();
       print $this->motor->loadedProperty; //print something
   }
}

class Motor
{   
    public function loadMotorData()
    {
        $this->motorDao = new MotorDao();
        $this->motorData = $this->motorDao->loadMotor();
        return $this->motorData;
    }
}

class MotorDao
{
    public function loadMotor()
    {
        $data = mysql_db_query($database, "SELECT...", $conn);
        return $data;
    }
}

It was mentioned that the above pattern is using dependency that closely ties my DAO to the business object. And that a DataMapper pattern will serve me better, because DataMapper will allow my business object develop independently over time.

Question:

Is there any particular problem or issue (with the way my code above is linked and structured) that will cause me problems? Anything related to dependencies linked incorrectly? If yes, where exactly and what are those problems (can you give me a hypothetical/concrete example)? Namely what problems can show up that will not show up in a properly-implemented DataMapper pattern?

I am seeking an answer that will most help me understand the relationship between my code and a DataMapper pattern. I see to understand pros/cons/ benefits/deficiencies betwen DataMapper and what I have above.

To contrast, DataMapper is something like this:

$product->motor = (new MotorMapper())->loadMotor();

class ProductMapper 
{
    function loadMotor()
    {
        $motor = db_query("SELECT ...");
        return $motor;
    }
}

Aucun commentaire:

Enregistrer un commentaire