ZF + 2原则:重模型类,或者轻量级模型+服务层? [英] ZF + Doctrine 2 : Heavy model classes or Lightweight model + Service layer?

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

问题描述

我整合的 Zend框架 2学说,然后我发现在服务层

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

现在我明白了(我错了?),我有2个可能的体系:

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


  • A 模式,其中类包含域逻辑,即性能+ getter / setter方法​​+复杂的方法

  • A 轻量化模型,其中类包含的属性+ getter / setter方法​​和服务层,包含域逻辑和修改模型类

  • 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和库本身,或委托给其他一些code。该控制器将永远不会知道存在。

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 + 2原则:重模型类,或者轻量级模型+服务层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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