I\'m trying to apply now what I learned about DDD and I\'m a little bit confused about the flow of dependencies in the Domain model.
My questions are:
Should an Entity be aware of Factories, Repositories, Services in the domain?
Notice the differente between interface and implementation: that's why you should use interface & implementations.
And FYI, Factories and Repositories are services after all, so you can generalize:
Easy
A domain service is one which is defined within the domain layer, though the implementation may be part of the infrastructure layer. A repository is a domain service whose implementation is indeed in the infrastructure layer, while a factory is also a domain service whose implementation is generally within the domain layer.
(Source: http://www.methodsandtools.com/archive/archive.php?id=97p2)
Should a Repository be aware of Services in the domain?
Usually no, why would it after all? The repository manage persistence. But I don't think that's "prohibited" because the infrastructure layer (persistence) knows about the domain layer.
Another thing that is bothering my mind is how to treat to collections when I want to add and entity to the collection.
Prefer the OOP approach when possible:
$article = new Article();
$article->addTag($tag);
$articleRepository->save($article);
I makes way more sense.
a domain service is any business logic that does not easily live within an entity.
(http://www.methodsandtools.com/archive/archive.php?id=97p2)
or also:
When a significant process or transformation in the domain is not a natural responsibility of an ENTITY or VALUE OBJECT, add an operation to the model as a standalone interface declared as a SERVICE.
(Eric Evans)
To sum up, create a domain service if you feel you need to, this is not automatic.