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

大城市里の小女人 提交于 2019-12-03 07:54:49

问题


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:


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.



来源:https://stackoverflow.com/questions/5850249/zf-doctrine-2-heavy-model-classes-or-lightweight-model-service-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!