samedi 9 janvier 2016

Data Mapper - should I use dependency injection?

Should I pass model as an dependency injection in data mapper pattern or should I declare the model inside the mapper class?

class Mapper
{
    public function __construct(
        $model
    )
    {
        $this->model = $model;
    }

    public function mapObject(array $row)
    {
        $this->model->setArticleId($row['article_id']) ;
        $this->model->setTitle($row['title']);
        $this->model->setDescription($row['description']);
        $this->model->setContent(isset($row['content']) ? $row['content'] : null);
        $this->model->setTemplate(isset($row['template']) ? $row['template']['path'] : null);

        return $this->model;
    }
}

or:

class Mapper
{
    public function mapObject(array $row)
    {
        $model = new Model;
        $model->setArticleId($row['article_id']) ;
        $model->setTitle($row['title']);
        $model->setDescription($row['description']);
        $model->setContent(isset($row['content']) ? $row['content'] : null);
        $model->setTemplate(isset($row['template']) ? $row['template']['path'] : null);

        return $model;
    }
}

Which one is correct?

Aucun commentaire:

Enregistrer un commentaire