简体   繁体   English

域驱动设计,映射器/存储库值高于/低于

[英]Domain driven design, mapper/repository value is higher/lower than

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. 映射器只是crud动作,而存储库看起来就像实体一样。

So it looks like something like 所以它看起来像

class UserMapper
{
    public function fetch($criteria)
    {
         // magic on the database
    }
}

class UserRepository
{
    public function getByName($name)
    {
        $this->mapper->fetch(array('name' =>  $name));
    }
}

Code will not work, but it gives a general idea 代码不起作用,但它提供了一个大致的想法

Now I come with the following problem 现在我遇到以下问题

Let's say I want the user with the name "john" and with gender "male". 假设我希望用户名为“john”,性别为“male”。 Should the Repository now have a function getByNameAndGender($name, $gender). 如果存储库现在具有函数getByNameAndGender($ name,$ gender)。 But then you would get soon a lot of functions because you need where gender, age and name etc. 但是你会很快获得许多功能,因为你需要性别,年龄和名字等。

Next is that all the examples i find only implement the following "value == value". 接下来是我发现的所有示例只实现了以下“value == value”。 But not a single example was found with fomething like "value < value" or something else. 但是没有找到像“值<值”之类的东西的单一例子。 So my question is how do you implement this. 所以我的问题是你如何实现这一点。 Because again I can see that you could make functions like 因为我再次看到你可以做出类似的功能

getWhereHeightIsLowerThan($height)
getWhereHeightIsLowerThanOrEquals($height)
getWhereHeightIs($height)
getWhereHeightIsGreaterThan($height)
getWhereHeightIsGreaterThanOrEquals($height)

Again 5 functions for something that should be like 1 function. 对于应该像1个函数的东西,还有5个函数。 How do you implement this in the repository / mapper pattern. 如何在存储库/映射器模式中实现它。 To make things even more complecated, I am not using a SQL database. 为了使事情更加复杂,我没有使用SQL数据库。 But MongoDB, now that shouldn't be a big issue. 但MongoDB,现在应该不是一个大问题。

-- -

Using the answer given by eulerfx i came with with following code 使用eulerfx给出的答案,我带有以下代码

use Application\\Specification\\AbstractSpecification; 使用Application \\ Specification \\ AbstractSpecification;

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->id = $id;
    }

    /**
     * @return mixed
     */
    public function getQuery()
    {
        return [
            '_id' => new \MongoId($this->id),
        ];
    }
}

I use php and so I can't use the words AND and OR so renamed them to ALSO and OF. 我使用PHP,所以我不能使用单词AND和OR,因此将它们重命名为ALSO和OF。

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. 这是我的需求不是最好的方法,所以我有方法getQuery而不是isSatisfied。

All the query logic is found in the specification. 所有查询逻辑都可以在规范中找到。 The repository builds up with the specification comon functions, like get by id. 存储库使用规范comon函数构建,例如get by id。

And the mapper is just the simple crud handler of the datase (fetch, fetchAll, count, insert, etc). mapper只是数据的简单crud处理程序(fetch,fetchAll,count,insert等)。

At the end I only have to change the getQuery methods if I want to change the database language (from mongo syntax to sql). 最后,如果我想更改数据库语言(从mongo语法到sql),我只需要更改getQuery方法。 Hope it helps some one 希望它有所帮助

It seems that you're trying to address the complexity in complex queries. 您似乎正试图解决复杂查询的复杂性。 For this, you can use the specification pattern . 为此,您可以使用规范模式 With the specification pattern, you'd have an object representing the query. 使用规范模式,您将拥有一个表示查询的对象。 This object would be composed of predicates about the entity at hand, such as WhereHeightIsLowerThan 3 . 该对象由关于手头实体的谓词组成,例如WhereHeightIsLowerThan 3 The repository would then build a SQL query or a MongoDB query based on this specification object. 然后,存储库将基于此规范对象构建SQL查询或MongoDB查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM