PHP中没有ORM映射的DDD值对象和实体 [英] DDD Value Objects and Entity Without ORM Mapping in PHP

查看:70
本文介绍了PHP中没有ORM映射的DDD值对象和实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,据我所知,DDD中的Entity与Value Object几乎相同,只是Entity具有身份.我读过的每一篇文章都说实体ID与任何ORM工具都具有ORM映射.但是我不想在实体中使用ORM映射.相反,我想使用存储库接口进行数据库操作而不进行映射.而且,在这种情况下,我被困在应该如何做的事情上.

First, as I know, Entity in DDD is almost same with Value Object except Entity has identity. Every article I have read say same thing that entity id has ORM mapping with any ORM tool. But I don’t want to use ORM mapping in Entity. Instead, I would like to do database operation with Repository Interfaces without mapping. And, in this case, I am stuck on how I should do this.

我将在下面用一个例子来解释我的想法

I will explain in my mind with an example below

假设我有一个TODO应用程序,并且TODO中有一些问题,每一个问题中都有一些答案.

Let’s assume I have a TODO application and there are some questions in the TODO and some answers in each those questions.

我们有3个值对象(或实体):去做待办事项TodoQuestionValue

We have 3 value object (or entity): Todo TodoQuestion TodoQuestionValue

现在,我以为我有一个TODO的值对象(或实体).该值对象具有一种获取问题的方法,该方法获取TodoQuestion值对象的数组.在TodoQuestion值对象内部,我们有一种方法来获取要获取TodoQuestionValue数组的问题的值.

Now, I thought that I have a value object (or entity) for TODO. This value object has a method to get questions that gets array of TodoQuestion value object. And inside of TodoQuestion value object we have a method to get values of questions that gets array of TodoQuestionValue.

<?php
class Todo{
   private int $id;
   /**
    * @param array<TodoQuestion> $questions
    */
   private array $questions;
   private TodoRepositoryInterface $repository;
   public function __constructor(TodoRepositoryInterface $repository){
      $this->repository = $repository;
   }
   public function getQuestions(){
      $this->questions = $this->repository->listQuestionsByTodoId($this->id);
   }
}

<?php
class TodoQuestion{
   private int $id;
   private string $question;
   /**
    * @param array<TodoQuestionValue> $values
    */
   private array $values;
   private TodoRepositoryInterface $repository;
   public function __constructor(TodoRepositoryInterface $repository){
      $this->repository = $repository;
   }
   public function getValues(){
      $this->values = $this->repository->listValuesByQuestionId($this->id);
   }
}

现在,我想听取您有关如何遵循DDD规则来塑造此结构的意见.

Now, I would like to get your opinions about how I could shape this structure by following the DDD rules.

谢谢.

推荐答案

假设我有一个TODO应用程序,并且TODO中有一些问题,每一个问题中都有一些答案.

Let’s assume I have a TODO application and there are some questions in the TODO and some answers in each those questions.

您只需要将此翻译为代码即可.这里唯一的实体是Todo.使其成为聚合根并为其创建存储库.

You just need to translate this into code. The only entity here is the Todo. Make it the aggregate root and create a repository for it.

<?php

class Todo
{
    private int $id;
    /**
     * @param array<TodoQuestion> $questions
     */
    private array $questions;

    public function getQuestions()
    {
        return $this->questions;
    }
}

问题有答案.您可以使用2个值对象进行建模:问与答.

The question has answers. You can model that with 2 value objects: Question and Answer.

<?php

class TodoQuestion
{
    private string $question;
    private array $values;
    /**
     * @var TodoAnswer[]
     */
    private array $answers;

    public function getValues()
    {
        return $this->values;
    }

    public function getAnswers(): array
    {
        return $this->answers;
    }
}

您的模型不应依赖于存储库.存储库任务是保存聚合的状态,并在查询聚合时重建相同的状态.

Your model should never depend on the repository. The repository task is to save the state of your aggregate and rebuild the same state when you query your aggregate.

interface TodoRepository {
    public function save(Todo $todo): void;

    public function todoOfId(TodoId $id): Todo;
}

您只需要这两种方法即可.如果要获取一个Todo的问题列表,只需获取Todo聚合根,然后调用-> getQuestions().在您的存储库实现中,您可以决定使用orm,编写原始查询,序列化聚合然后保存它……无限可能就是确保您的模型与这些基础结构无关.

You don't need more than this 2 methods. If you want to have the list of questions for one Todo you just get the Todo aggregate root and then call ->getQuestions(). In your repository implementation you can decide to use the orm, write raw queries, serialize the aggregate and then save it... The possibilities are endless just make sure you keep your model decoupled from these infrastructure concerns.

这篇关于PHP中没有ORM映射的DDD值对象和实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆