I'm trying to work effectively with DDD and Doctrine2 on a project with lot of business logic.
I understand that we need decouple the domain objects from other concepts related to the system, i.e. in a layered architecture, "the domain layer" must be isolate from other layers, like the persistence layer/service (Doctrine2 for me).
But there is one thing it's hard to understand for me: in several code examples of DDD with Doctrine 2, aggregates in domain entities are managed with Doctrine ArrayCollection, I found this kind of code :
namespace Acme\Domain\Model\Users;
use Doctrine\Common\Collections\ArrayCollection;
class User {
//...
/**
* Collection of Roles
*
* @var Collection of Roles
*/
protected $roles;
/**
* Constructor.
*/
public function __construct()
{
$this->createdAt = new \DateTime();
$this->roles = new ArrayCollection();
}
public function getRoles()
{
return $this->roles;
}
//...
}
For me, this implementation create a high coupling between domain models and the persistence service, Doctrine2.
On the other hand, if DDD Entity and Doctrine Entity classes are decoupled, there is to many layers/classes, in my opinion.
What do you think? Is there a better way to avoid/handle this?