Reputation: 77
I want to use Doctrine for data persistence in a Domain Driven Design approach. But what are bothering me is the fact that the association mapping should be inside the entity. Isn't that a bad practice in terms of a pure DDD? Because until I get into the persistence issue, my domain entities were so clean, and now they have a lot of comments in its properties. Like this:
<?php
namespace Domain\Model;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\SequenceGenerator(sequenceName="user_id_seq", allocationSize=1, initialValue=1)
* @ORM\Column(type="integer")
*/
private $id;
...
Besides using these comments, I also have to have the use Doctrine\ORM\Mapping as ORM;
. In that way, the infrastructure are not keeping separate from domain.
Is there some way to move this mapping to a config file? I'm using ZF2.
Upvotes: 4
Views: 2377
Reputation: 1
Onion Architecture has the answer to your question and is one of the recommended architecture approaches for DDD. The idea I that you keep Domain Layer only contains business logic. The infrasctruture layer will have the repository. It the job of the repository to persist the changes made in the domain objects. Hence, if you are using ORM, this is the layer where you should maintain your mapping. Here's is a great post on how to implement Repository in DDD, hope it helps,
https://codingcraft.wordpress.com/2015/10/12/implementing-repository-in-ddd-part-1/
Upvotes: 0
Reputation: 18034
Yes, this is bad from a DDD perspective. There are two solutions:
Use an external mapping mechanism, such as the one described by @Cerad. This frees the domain model from persistence concerns, but it may also be a pain to manage. After all, many projects moved away from XML-based persistence configuration for a reason.
Create a separate persistence model. This model is dead-simple and contains no logic at all, just data. Then, create a mapper to map between the two.
Upvotes: 4