ZF + Doctrine 2 : Heavy model classes or Lightweig

2019-03-11 11:13发布

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.

1条回答
放我归山
2楼-- · 2019-03-11 11:30

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.

查看更多
登录 后发表回答