Doctrine2 Best Practice, Should Entities use Services?

My advice would be to put all the query logic into an EntityRepository and then make an interface out of it sort of like: class BlogPostRepository extends EntityRepository implements IBlogPostRepository {} that way you can use the interface in your unit-tests for the service objects and no dependency on the EntityManager is required.

1 Was wondering the best way to test once repositories/service layers/etc were added to the equation. – Tim Lytle Dec 19 '10 at 21:52.

I vote for the service layer. I've often struggled with trying to add as much logic on the Entity itself, and simply frustrated myself. Without access to the EntityManager, you're simply not able to perform query logic, and you'll find yourself using a lot of O(n) operations or lazy-loading entire relationship sets when you only need a few records (which is super lame when compared to all the advantages DQL offers).

If you need some assistance getting over the idea that the Anemic Domain Model is always an anti-pattern, see this presentation by Matthew Weier O'Phinney or this question. And while I could be misinterpreting the terminology, I'm not completely convinced that Entities have to be the only objects allowed in your Domain Model. I would easily consider that the sum of Entity objects and their Services constitutes the Model.

I think the anti-pattern arises when you end up writing a service layer that pays little to no attention to separation of concerns. I've often flirted with the idea of having all my entity objects proxy some methods to the service layer: public function addVote($vote) { $this->_service->addVoteToThing($vote, $thing); } However, since Doctrine does not have any kind callback event system on object hydration, I haven't found an elegant way to inject the service object.

I will checkout the links - I agree that the Service Layer can (should) be considered part of the Model. I think Fowler says the same in his Anemic Domain Model post). While I'd (currently) favor letting the entities handle adding the vote internal, I'd proxy to the service layer for finding/deleting a vote - instead of the crazy search through the collection.

I guess the question is - how to inject the service object. Perhaps a static function like $em->getRepository()? – Tim Lytle Dec 16 '10 at 22:13 Got a chance to go through that presentation - really good information.

Reminds me of some Zend Framework architecture (no surprise there). Still processing how a heaver service layer would effect ease of unit testing. Again, great link.

– Tim Lytle Dec 17 '10 at 3:08 I'm interested in you approach here. I'm also toying with the idea of having a service as a property of my entity and then implementing methods like 'save' on the entities themselves (which use the service to handle the persistance). This seems a bit activeRecord(ish) which is why I habn't done it.Is the alternative to use a Service Object but pass the entities INTO in instead?

– kissmyface Jun 24 at 8:36 @kissmyface Your entity is just a Data Container, you should not rely on it this kind of dependence. Even if you use some type of dependency injection, you will end up having an entity full of methods to manipulate data and often repeating the method of its own class of service. Why instead of having the class of service within your entity, you do not have their entity within the class of service?

– Jonathan Aug 9 at 15:52.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions