Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF + Doctrine 2 : Heavy model classes or Lightweight model + Service layer?

I am integrating Zend Framework and Doctrine 2, and I am discovering the Service layer.

Now I understand (am I wrong ?) that I have 2 architectures possible :

  • A model, where classes contain domain logic, i.e. properties + getters/setters + complex methods
  • A lightweight model, where classes contain properties + getters/setters and a Service layer, containing domain logic, and modifying the model classes

What are the pros/cons of each ?

It seems weird to me to lose OOP by putting domain logic as external to the model, so I don't understand why use a Service layer.

like image 299
Matthieu Napoli Avatar asked May 01 '11 17:05

Matthieu Napoli


1 Answers

What makes you think your Service Layer is external to your model? It isn't. In fact, it's a central part of your model, along with entities, repositories, etc.

If you're using Doctine2, you'll want a service layer. One reason is that you don't want your Entities knowing about the EntityManager (hurts testability). Another is that you also don't want your controllers driving the EM (it's not the controllers job to know about persistence).

I typically use an architecture where the service layer is the controller's interface to the model. The service layer exposes functions that operate on entities (either taking them as parameters, or returning them, or both). Persistence of entities is hidden by the service layer. Either the service class drives the EM and repositories itself, or delegates it to some other code that the controller will never know exists.

So the service layer provides and API that controllers can to use to manipulate your business data.

like image 110
timdev Avatar answered Oct 05 '22 23:10

timdev