Note that there are some explanatory texts on larger screens.

plurals
  1. PODomain driven design, mapper/repository value is higher/lower than
    primarykey
    data
    text
    <p>In a project I am working on we use domain driven design. With this we also use the mapper and repository pattern. Where the mapper are just the crud action, and the repository are look like the entity.</p> <p>So it looks like something like</p> <pre><code>class UserMapper { public function fetch($criteria) { // magic on the database } } class UserRepository { public function getByName($name) { $this-&gt;mapper-&gt;fetch(array('name' =&gt; $name)); } } </code></pre> <p>Code will not work, but it gives a general idea</p> <p>Now I come with the following problem</p> <p>Let's say I want the user with the name "john" and with gender "male". Should the Repository now have a function getByNameAndGender($name, $gender). But then you would get soon a lot of functions because you need where gender, age and name etc.</p> <p>Next is that all the examples i find only implement the following "value == value". But not a single example was found with fomething like "value &lt; value" or something else. So my question is how do you implement this. Because again I can see that you could make functions like</p> <pre><code>getWhereHeightIsLowerThan($height) getWhereHeightIsLowerThanOrEquals($height) getWhereHeightIs($height) getWhereHeightIsGreaterThan($height) getWhereHeightIsGreaterThanOrEquals($height) </code></pre> <p>Again 5 functions for something that should be like 1 function. How do you implement this in the repository / mapper pattern. To make things even more complecated, I am not using a SQL database. But MongoDB, now that shouldn't be a big issue.</p> <p>-- </p> <p>Using the answer given by eulerfx i came with with following code <p>use Application\Specification\AbstractSpecification;</p> <pre><code>class IdIsSpecification extends AbstractSpecification { /** @var string */ protected $id; /** * @param $id * @throws \InvalidArgumentException */ function __construct($id) { if(!is_string($id)) { throw new \InvalidArgumentException('Expects string, given ' . gettype($id)); } $this-&gt;id = $id; } /** * @return mixed */ public function getQuery() { return [ '_id' =&gt; new \MongoId($this-&gt;id), ]; } } </code></pre> <p>I use php and so I can't use the words AND and OR so renamed them to ALSO and OF.</p> <p>Also the specification patterns look over a dataset that is throw at it. This is for my needs not the best way, so I have the method getQuery instead of isSatisfied.</p> <p>All the query logic is found in the specification. The repository builds up with the specification comon functions, like get by id.</p> <p>And the mapper is just the simple crud handler of the datase (fetch, fetchAll, count, insert, etc).</p> <p>At the end I only have to change the getQuery methods if I want to change the database language (from mongo syntax to sql). Hope it helps some one</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload