架构比 MVC 更适合 Web 应用程序? [英] Architecture more suitable for web apps than MVC?

查看:25
本文介绍了架构比 MVC 更适合 Web 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为我的新工作学习 Zend 及其 MVC 应用程序结构,发现使用它只是困扰我,原因我无法完全理解.然后在我的学习过程中,我遇到了诸如 MVC:No Silver Bullet此播客 关于 MVC 和 Web 应用程序的主题.播客中的那个人针对 MVC 作为 Web 应用程序架构提出了一个很好的案例,并指出了很多困扰我的问题.

I've been learning Zend and its MVC application structure for my new job, and found that working with it just bothered me for reasons I couldn't quite put my finger on. Then during the course of my studies I came across articles such as MVC: No Silver Bullet and this podcast on the topic of MVC and web applications. The guy in the podcast made a very good case against MVC as a web application architecture and nailed a lot of what was bugging me on the head.

然而,问题仍然存在,如果 MVC 不是真正适合 Web 应用程序,那什么是合适的?

However, the question remains, if MVC isn't really a good fit for web applications, what is?

推荐答案

这完全取决于您的编码风格.秘密在这里:用 PHP 编写经典的 MVC 是不可能的.

It all depends on your coding style. Here's the secret: It is impossible to write classical MVC in PHP.

任何声称可以做到的框架都是在骗你.现实情况是,框架本身甚至无法实现 MVC——您的代码可以.但我想这不是一个好的营销宣传.

Any framework which claims you can is lying to you. The reality is that frameworks themselves cannot even implement MVC -- your code can. But that's not as good a marketing pitch, I guess.

要实现经典的 MVC,您首先需要拥有持久模型.此外,Model 应该通知 View 有关更改(观察者模式),这在您的普通 PHP 页面中也是不可能的(如果您使用套接字,您可以做一些接近经典 MVC 的事情,但这对于真正的网站是不切实际的).

To implement a classical MVC it would require for you to have persistent Models to begin with. Additionally, Model should inform View about the changes (observer pattern), which too is impossible in your vanilla PHP page (you can do something close to classical MVC, if you use sockets, but that's impractical for real website).

在 Web 开发中,您实际上还有 4 种其他受 MVC 启发的解决方案:

In web development you actually have 4 other MVC-inspired solutions:

  • Model2 MVC:视图从模型请求数据,然后决定如何呈现它以及使用哪些模板.控制器负责改变视图和模型的状态.

  • Model2 MVC: View is requesting data from the Model and then deciding how to render it and which templates to use. Controller is responsible for changing the state of both View and Model.

MVVM:Controller 被替换为 ViewModel,它负责 View 的期望和 Models 逻辑之间的转换.视图从控制器请求数据,控制器转换请求以便模型可以理解它.

MVVM: Controller is swapped out for a ViewModel, which is responsible for the translation between View's expectations and Models's logic. View requests data from controller, which translates the request so that Model can understand it.

当您无法控制视图或模型层时,通常会使用它.

Most often you would use this when you have no control over either views or the model layer.

MVP(php 框架称之为MVC"):Presenter 从 Model 请求信息,收集它,修改它,并将它传递给被动 View.

MVP (what php frameworks call "MVC"): Presenter requests information from Model, collects it, modifies it, and passes it to the passive View.

要探索这种模式,我建议您从本出版物开始.它会详细解释.

To explore this pattern, I would recommend for you begin with this publication. It will explain it in detail.

HMVC(或 PAC):与 Model2 不同,控制器具有执行子控制器的能力.每个都有自己的 M、V 和 C 三元组.您获得了模块化和可维护性,但在性能上会受到一些打击.

HMVC (or PAC): differs from Model2 with ability of a controller to execute sub-controllers. Each with own triad of M, V and C. You gain modularity and maintainability, but pay with some hit in performance.

无论如何.底线是:你还没有真正使用过 MVC.

Anyway. The bottom line is: you haven't really used MVC.

但如果你厌倦了所有类似 MVC 的结构,你可以看看:

But if you are sick of all the MVC-like structures, you can look into:

  • 事件驱动架构
  • n 层架构

然后总是存在 DCI 范式,但在应用时存在一些问题到 PHP(你不能强制转换到 PHP 中的一个类......不是没有丑陋的黑客).

And then there is always the DCI paradigm, but it has some issues when applied to PHP (you cannot cast to a class in PHP .. not without ugly hacks).

这篇关于架构比 MVC 更适合 Web 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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