Data Mapper Pattern: Complexe query from Service Layer

主宰稳场 提交于 2019-12-03 07:24:18

Indeed, for two conditions, it inadvisable to query on one condition and then filter on your other while you iterate. It leaves you no clear approach for more than two conditions. And implementing pagination adapters becomes pretty messy.

Seems to me that the issue is that your mapper fetch() method supports permits only a single condition. So:

  1. Modify the signature to support an array of conditions.

  2. Alternatively, you could create a separate mapper method for each enhanced fetch method, ex: fetchByHairAndAge($hair, $age), etc.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!