MVC + 服务层在 Zend 或 PHP 中常见吗?

时间:2023-02-28
本文介绍了MVC + 服务层在 Zend 或 PHP 中常见吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可能听说过胖模型/瘦控制器与瘦模型/胖控制器的区别.我最近听说你可以在模型中的一些逻辑进入服务层的地方设置一些东西.这有多常见?你知道(或能想到)任何可以说明它的真实例子吗?

You've probably heard of the Fat Model/Thin Controller vs. Thin Model/Fat Controller distinction. I recently heard that you can have something in between where some of the logic from the model goes into a service layer. How common is this? and do you know of (or can think of) any real examples that illustrate it?

推荐答案

Martin Fowler 描述了服务层 他的伟大著作企业应用程序架构模式的模式.如果你像你问的那样关心问题,你应该阅读这本书.

Martin Fowler describes the Service Layer pattern of his great book Patterns of Enterprise Application Architecture. If you care about questions like the one you asked, you should read this book.

我想到的一个用途是管理数据库事务.有些人试图在他们的领域模型中封装启动和提交事务.但是当域模型调用其他也尝试启动和提交数据库事务的域模型时,他们会感到困惑.那么哪个模型真正可以决定事务是提交还是回滚?如果不同客户以不同方式使用给定模型,您会怎么做?

One use that comes to my mind is managing database transactions. Some people try to encapsulate starting and committing transactions in their domain models. But then they get confused when domain models invoke other domain models that also try to start and commit db transactions. So which model really gets to decide if a transaction is committed or rolled back? And what do you do if a given model is used in different ways by different clients?

服务层是一个解决方案,因为您可以在该层开始和提交涉及多个领域模型的工作.

The Service Layer is a solution for this, because this is the layer in which you can start and commit work that involves multiple domain models.

至于这种情况有多普遍,我认为根本不常见.大多数使用 Zend Framework(或任何其他 PHP 或 Ruby 框架)的人刚刚从Active Record 解决一切"转向新的闪亮的Data Mapper 解决一切".这个社区似乎每五年只学习一种新模式.他们暂时无法进入服务层.

As for how common this is, I don't think it's common at all. Most people using Zend Framework (or any other PHP or Ruby framework) have just barely moved from "Active Record solves everything" to the new shiny, "Data Mapper solves everything." It seems this community learns only one new pattern every five years. They won't get to Service Layer for a while.

来自@ktutnik 的重新评论:

Re comment from @ktutnik:

不,服务层模式与存储库模式不同.存储库是关于抽象数据库访问,因此您可以使用像集合这样的数据库.服务层是关于封装复杂的应用程序操作.

No, the Service Layer pattern is different from Repository pattern. Repository is about abstracting database access so you can use a database like a Collection. Service Layer is about encapsulating complex application operations.

另一种思考它们的方式是它们与领域模型的关系.Repository 用于域模型和数据库之间.而服务层使用一个或多个领域模型.

Another way of thinking about them is their relationship to the Domain Model. The Repository is used between the Domain Model and the database. Whereas the Service Layer uses one or more Domain Models.

Service Layer --->  Domain Model(s) ---> Repository ---> DBAL

这篇关于MVC + 服务层在 Zend 或 PHP 中常见吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:PHP MVC 方法中的 Hello World 示例 下一篇:有一个网站的单一入口点.坏的?好的?没问题?

相关文章

最新文章