Data Mapper Pattern: Complexe query from Service Layer

前端 未结 2 1519
梦毁少年i
梦毁少年i 2021-02-08 20:35

I\'m sing the Data Mapper Pattern in Zend Framework. This works well so far, but now I got to a point where I need your help/opinion. So let\'s start with the Code:

We g

2条回答
  •  情歌与酒
    2021-02-08 20:58

    In my limited experience with data mappers I have found the following approach to work quite well for the scenarios I have encountered so far:

    public function getPeopleByHaircolorAndAge($haircolor, $age, $limit=null, $offset=null)
    {
        $people = new PersonCollection;
        $people->filterByHaircolor($haircolor);
        $people->filterByAge($age);
        $people->setLimit($limit);
        $people->setOffset($offset);
    
        $personCollectionMapper = new PersonCollectionMapper;
        $personCollectionMapper->fetch($people);
    
        return $people;
    }
    

    By instantiating the domain object first I have the option to set filters and other variables that the data mapper can read from the object when it is injected into the mapper.

    To me this has been the superior approach so far compared to having multiple mapper methods that returns a domain object.

提交回复
热议问题