在哪里放置功能?在实体或服务?

时间:2016-04-29 08:50:11

标签: php entity-framework doctrine-orm domain-driven-design

我有可以归档的实体。来自当前对象的所有数据都将复制到新的归档对象,并删除当前对象。现在它在实体中实现:

class Entity
{
...
    public function archive($em)
    {
        $objectArchive = new EntityArchive($this->toArray()); // all archive object properties are set in constructor
        $em->persist($objectArchive);

        $em->remove($this);

        return $objectArchive;
    }
...
}

最近我读过,修改多个实体的功能应该放在服务中,现在我很困惑。我应该将此功能移至服务中吗?像这样:

class ServiceEntityArchiver
{
    private $em;

    public function __construct($em)
    {
        $this->em = $em;
    }

    public function archive($object)
    {
            $objectArchive = new EntityArchive($object->toArray());
            $this->em->persist($objectArchive);

            $this->em->remove($object);

            return $objectArchive;
    }
}

您如何看待这个?

0 个答案:

没有答案