mardi 6 octobre 2015

Must I remove data dependency out of my Controllers (and into Factories)?

This question can be viewed through a prism of ZF2 + Doctrine + MVC programming practices, or it can be viewed through just an OOP perspective.

My concern is about Separation of Concerns, and on removing dependencies.

I am using code in my controllers that goes something like this:

class Controller
{ 
    private $em; //entityManager 

    function __construct()
    {
        $this->em = DoctrineConnector::getEntityManager();
    }

    function indexAction()
    {
        //Input
        $inputParameter = filter_input(...);

        //request for Data
        $queryBuilder = $this->em->createQuery(...)
                             ->setParameter('param', $inputParameter);
        $query = $queryBuilder->getQuery();
        //$services is the user-defined data type requested
        $services = $query->getResult();

        //use data to produce a view model
        $view = new ViewModel();
        $view->setVariables(array('services' => $services));
        return $view;
    }
}

I am not entirely comfortable with the above and wanted a second opinion. For one, my EntityManager is part of the class, so my class is cognizant of the entity manager construct, when I think it should not be a part of the controller. Do I perhaps use a Factory or Builder design pattern to help me create my Controller class?

If I do, I can move my em (entityManager) construct into the Factory pattern and create and populate my Controller inside the Factory. Then, the Controller can have a private variable $services instead.

i.e.

class Controller
{ 
    private $services;

    function setServices($services)
    {
        $this->services = $services;
    }

    function indexAction()
    {
        //use data to produce a view model
        $view = new ViewModel();
        $view->setVariables(array('services' => $this->services));
        return $view;
    }
}

class FactoryMethod
{
    function createController()
    {
        //Input
        $inputParameter = filter_input(INPUT_GET...);

        //request for Data
        $queryBuilder = $this->em->createQuery(...)
                             ->setParameter('param', $inputParameter);
        $query = $queryBuilder->getQuery();
        //$services is the user-defined data type requested
        $services = $query->getResult();

        //create and return Controller instance
        $controller = new Controller();
        $controller->setServices($services);
        return $controller;
    }
}

I typically tried to do this PHP's mysql extension to remove dependency on data out of my various objects. I am using Doctrine2 now which is an ORM, and wondering if I should keep doing the same thing (namely preferring 2nd example rather than the first...

Question:

I can write code both ways. It works essentially the same. My question is -- is the code, as it is written in my 2nd example preferred more than the code as it is written in my first?

Aucun commentaire:

Enregistrer un commentaire