drep94225 2015-06-30 08:01
浏览 67
已采纳

正确的DDD方式在oneToMany关系中创建和删除子项?

I am struggling to find the proper DDD way to have a parent/child oneToMany relationship that:

  • Ensures entities cannot exist in invalid state
  • Exposes no unwanted methods (i.e. a clean API)

I am using PHP and Doctrine2 but I guess this applies to many other languages/platforms as well. Here's my base entity code. I have Parent and Child objects. A Child cannot exist without a parent.

/**
 * @ORM\Entity
 */
class ParentClass
{
    /**
     * @ORM\OneToMany(targetEntity="Child", mappedBy="parent", orphanRemoval=true, cascade={"persist", "remove"})
     */
    private $children;
}

/**
 * @ORM\Entity
 */
class Child
{
    /**
     * @ORM\ManyToOne(targetEntity="Base", inversedBy="children")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
     */
    private $parent;
}

But how should I create and remove child entities?

To enfore consistency I could have the parent as a constructor parameter for Child:

class ParentClass
{
    public function addChild(Child $child)
    {
        $this->children[] = $child;
    }

    public function removeChild(Child $child)
    {
        $this->children->removeElement($child);
    }
}

class Child
{
    public function __construct(ParentClass $parent)
    {
        $this->parent = $parent;
        $this->parent->addChild($this);
    }
}

$parent = new ParentClass();
$child = new Child($parent);

The problem with this is that it exposes addChild which really shouldn't be used by developers now. It would need a whole load of extra checks to ensure you cannot move children between parents.

As an alternative I could use a setter:

class ParentClass
{
    public function addChild(Child $child)
    {
        $child->setParent($this);
        $this->children[] = $child;
    }

    public function removeChild(Child $child)
    {
        $this->children->removeElement($child);
    }
}

class Child
{
    public function setParent(ParentClass $parent)
    {
        $this->parent = $parent;
    }
}

$parent = new ParentClass();
$parent->addChild(new Child());

The problem here is that Child would be in an invalid state until you call addChild.

A third option could be to have addChild create a new child:

class ParentClass
{
    public function addChild()
    {
        $child = new Child($parent);
        $this->children[] = $child;
        return $child;
    }

    public function removeChild(Child $child)
    {
        $this->children->removeElement($child);
    }
}

class Child
{
    public function __construct(ParentClass $parent)
    {
        $this->parent = $parent;
    }
}

$parent = new ParentClass();
$child = $parent->addChild();

The problem with this is that the child constructor is exposed to developers. Also, my (Symfony) form library is probably going to hate me, causing me to have a bunch of DTO's and mappers just for a simple use case.

There are probably even more possible ways to handle this. What is the preferred way to ensure a clean domain model?

  • 写回答

1条回答 默认 最新

  • doudi8519 2015-06-30 13:19
    关注

    Ensuring a clean Domain model means you ignore everything db related, like one to many relationships. Your parent/child issue is a smell, a hint that you're using db driven design.

    At the Domain level, the Aggregate Root (AR) acts as the 'parent', although the term is wrong. An Aggregate represents a domain concept, while the AR is in charge of ensuring its consistency . The 'children' are elements without which the concept can't exist. You'll always be using the AR to work with the 'children', because this is the only way you can ensure consistency. Basically, the AR is in charge of creating the actual 'children' objects.

    Treating an AR like a container is an anti-pattern. Has in DDD means, it's defined by, not it contains . I've wrote some posts about it some years ago but they're still valid.

    Your Symfony form library shouldn't hate you, because that's a UI concern, not a Domain one. You should use a specific view model / input that will be sent to an application service which will use it to create/update the domain model. If you can use directly the domain model for UI purposes, then maybe all you have is just a CRUD app where you don't need DDD.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄