php – 如何在Symfony2项目中使用Doctrine2实现DDD?
作者:互联网
好吧,我正在尝试将自己介绍给DDD,我对它很新,一些概念仍然不清楚.
这是我到目前为止所理解的:
>该域基本上是关于数据
>持久层不依赖于域,但业务逻辑事务可能是.
使用Doctrine2时,我们使用EntityRepository或CustomEntityRepository实现.
在DDD中,Repository模式看起来有点不同,我看过.NET& Java示例以及来自DDD邮件列表的消息,人们倾向于认为存储库应该返回QueryObject,在Doctrine2中,我计划从我的存储库返回QueryBuilder实例.
因此,为了隐藏使用QueryBuilder的复杂性,然后查询一个Hydrated结果集,我实现了另一个我称为Manager的服务层.
以下是我的域名的样子:
src/Domain/
├── Entity
│ ├── AbstractComment.php
│ ├── Comment.php
├── Manager
│ ├── CommentManager.php
└── Repository
└── CommentRepository.php
Entity文件夹只是纯POPO.
CommentRepository看起来像这样:
<?php
namespace Acme\Domain\Repository;
use Doctrine\Common\Collections\Criteria;
class CommentRepository
{
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* @param $id
*
* @return \Doctrine\ORM\QueryBuilder
*/
public function findOneById($id)
{
$qb = $this->getEntityManager()
->getRepository('Acme:Domain\Entity\Comment')
->createQueryBuilder('c');
$criteria = new Criteria();
$criteria->andWhere(
$criteria->expr()->eq('c.id', ':id')
);
$qb->addCriteria($criteria);
$qb->setParameter('id', $id);
return $qb;
}
}
而CommentManager:
<?php
namespace Acme\Domain\Manager;
class CommentManager
{
protected $repository;
public function __construct(CommentRepository $repository)
{
$this->repository = $repository;
}
public function findOneById($id)
{
return $this->repository->findOneById($id)->getQuery()->getOneOrNullResult();
}
}
>这是管理“实体”的正确方法吗?
>遵循这样的模式,我在哪里必须处理持久性?
我的意思是,如果我是对的,存储库基本上就像一个集合,因此它应该提供add(Entity $e)和remove(Entity $e)方法,但是我在哪里实际持有该实体?
在add()和amp;中进行安全是否安全remove()方法?添加save()方法来处理更新是否更好?
感谢您的时间.
解决方法:
我用Symfony2开始了一个关于DDD的系列,应该回答你的问题:http://williamdurand.fr/2013/08/07/ddd-with-symfony2-folder-structure-and-code-first/.
I mean, if I’m right, the repository is basically like a collection
是.
therefore it should provide add(Entity $e) and remove(Entity $e) methods
是.
but where do I actually persist the entity?
在这个存储库中.但是,这可能不是Doctrine存储库. Doctrine使用Entity / Repository术语,但它们在DDD中的含义不同.
标签:ddd-repositories,doctrine-orm,php,symfony,domain-driven-design 来源: https://codeday.me/bug/20190725/1535636.html