How do I implement DDD with Doctrine2 in a Symfony2 project?

Well, I am trying to introduce myself as DDD, I am very new to this and some concepts are not clear yet.

Here's what I've figured out so far:

  • Domain is mostly data related
  • The persistence layer is not domain bound, but the business logic transaction can be.

When using Doctrine2, we are using either EntityRepository or CustomEntityRepository implementation.

In DDD, the repository pattern seems a bit different, I've looked at .NET and Java examples and posts from the DDD mailing list, and people tend to argue that the repository should return a QueryObject, in Doctrine2, I'm a project to return a QueryBuilder instance from my repository.

So to hide the complexity of working with a QueryBuilder and then a Query and then a hydrated result set, I implemented another service layer, which I called a manager.

This is what my domain looks like:

src/Domain/
β”œβ”€β”€ Entity
β”‚   β”œβ”€β”€ AbstractComment.php
β”‚   β”œβ”€β”€ Comment.php
β”œβ”€β”€ Manager
β”‚   β”œβ”€β”€ CommentManager.php
└── Repository
    └── CommentRepository.php

      

The Entity folder is just pure POPO.

CommentRepository

as follows:

<?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;
    }
}

      

And 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();
    }
}

      

  • Is this the correct approach for managing "entities"?
  • Following a pattern like this, where do I need to handle persistence?

I mean, if I'm right, a repository is basically like a collection, so it should provide methods add(Entity $e)

and remove(Entity $e)

, but where do I actually store the object?

Is it safe to do this in methods add()

and remove()

? Is it better to add a method save()

to handle updates?

Thank you for your time.

+3


source to share


1 answer


I started a DDD series with Symfony2 that should answer your questions: http://williamdurand.fr/2013/08/07/ddd-with-symfony2-folder-structure-and-code-first/ .

I mean if I am correct the repository is basically like a collection

Yes.

so it should provide add (Entity $ e) and remove (Entity $ e) methods



Yes.

but where am I actually saving the object?

In this repository. However, this is probably not a Doctrine repository. Doctrine uses the terms Entity / Repository, but they don't have the same meaning in DDD.

+8


source







All Articles