ZF + Doctrine 2:重模型类还是轻量模型 + 服务层? [英] ZF + Doctrine 2 : Heavy model classes or Lightweight model + Service layer?

查看:15
本文介绍了ZF + Doctrine 2:重模型类还是轻量模型 + 服务层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在集成 Zend FrameworkDoctrine 2,并且我正在发现服务层.

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 :

  • 一个模型,其中的类包含域逻辑,即属性 + getter/setter + 复杂方法
  • 一个轻量级模型,其中的类包含属性 + getter/setter 和一个 Service 层,包含域逻辑和修改模型类
  • 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 ?

通过将域逻辑作为模型的外部来失去 OOP 对我来说似乎很奇怪,所以我不明白为什么要使用服务层.

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.

推荐答案

是什么让您认为您的服务层是您的模型的外部?不是.事实上,它是模型的核心部分,还有实体、存储库等.

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.

如果您使用 Doctine2,您将需要一个服务层.原因之一是您不希望您的实体了解 EntityManager(损害可测试性).另一个是您也不希望您的控制器驱动 EM(了解持久性不是控制器的工作).

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).

我通常使用一种架构,其中服务层是控制器与模型的接口.服务层公开对实体进行操作的函数(将它们作为参数,或返回它们,或两者兼而有之).实体的持久性被服务层隐藏.服务类要么驱动 EM 和存储库本身,要么将其委托给控制器永远不会知道存在的其他一些代码.

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.

因此,服务层提供了控制器可以用来操作您的业务数据的 API.

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

这篇关于ZF + Doctrine 2:重模型类还是轻量模型 + 服务层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆