架构比 MVC 更适合 Web 应用程序?

发布于 2024-12-07 10:53:01 字数 340 浏览 2 评论 0原文

为了我的新工作,我一直在学习 Zend 及其 MVC 应用程序结构,并且发现使用它只是让我感到困扰,因为我不太明白其中的原因。然后在我的学习过程中,我遇到了诸如 MVC: No Silver Bullet这个播客主题是 MVC 和 Web 应用程序。播客中的那个人很好地反对了 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落叶缤纷 2024-12-14 10:53:01

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

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

要实现经典的 MVC,您首先需要有持久模型。此外,模型应该通知视图有关更改(观察者模式)的信息,这在普通 PHP 页面中也是不可能的(如果使用套接字,则可以执行类似于经典 MVC 的操作,但这对于真实网站来说是不切实际的)。

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

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

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

    当您无法控制视图或模型层时,通常会使用此选项。

  • MVP(php框架称之为“MVC”):Presenter从Model请求信息,收集信息,修改信息,然后将其传递给被动视图。

    要探索这种模式,我建议您从此出版物开始。它将详细解释它。

  • HMVC(或 PAC):与 Model2 不同,具有控制器执行子控制器的能力。每个都有自己的 M、V 和 C 三位一体。您可以获得模块化和可维护性,但性能会受到一些影响。

反正。底线是:您还没有真正使用过 MVC。

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

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

然后总是有 DCI 范例,但它在应用于 PHP 时存在一些问题(你不能转换为 PHP 中的类......并非没有丑陋的黑客)。

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

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.

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

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

  • 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 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 (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 (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.

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

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

  • event driven architectures
  • n-Tier architecture

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

年华零落成诗 2024-12-14 10:53:01

根据我的经验,在进行 Web 开发时,从 MVC 架构中获得的好处远远超过其成本和明显的开销。

对于刚开始使用复杂的 MVC 框架的人来说,付出额外的努力来分离三层并很好地了解什么属于哪里(有些事情是显而易见的,有些事情可能相当边界)可能有点令人畏惧并且往往是很好的讨论话题)。我认为从长远来看,这种成本是值得的,特别是如果您期望您的应用程序在合理的时间内增长或维护。

我曾经遇到过这样的情况:创建一个新的 API 以允许其他客户端连接到现有的 Web 应用程序的成本非常低,这是由于各层之间的良好分离:业务逻辑根本没有连接到演示文稿,因此这是蛋糕。

在当前的MVC框架生态系统中,我相信您的里程可能会有很大差异,因为原理是通用的,但之间存在很多差异,例如Zend、Django、RoR和SpringMVC。

如果真的有其他好的替代方案可以替代这个范例......我对答案很感兴趣!

抱歉,文字墙有点小!

From my experience, the benefits you get from an MVC architecture far outweighs its costs and apparent overhead when developing for the web.

For someone starting out with a complex MVC framework, it can be a little daunting to make the extra effort of separating the three layers, and getting a good feel as to what belongs where (some things are obvious, others can be quite border-line and tend to be good topics of discussion). I think this cost pays for itself in the long run, especially if you're expecting your application to grow or to be maintained over a reasonable period of time.

I've had situations where the cost of creating a new API to allow other clients to connect to an existing web application was extremely low, due to good separation of the layers: the business logic wasn't at all connected to the presentation, so it was cake.

In the current MVC framework eco-system I believe your mileage may vary greatly, since the principles are common, but there are alot of differences between, for instance, Zend, Django, RoR and SpringMVC.

If there are truly other good alternatives to this paradigm out there... I'm quite interested in the answers!

Sorry for the slight wall of text!

兔小萌 2024-12-14 10:53:01

我认为这取决于您个人想要做什么。 Magenta 非常成功地使用了 MVC,并且它使得添加新功能或修改现有功能变得相当容易。

当然,如果您试图让事情变得相当简单,那么使用 MVC 架构可能就有点矫枉过正了。

I think it would depend on what you're trying to do, personally. Magenta uses MVC pretty successfully, and it makes it fairly easy to add new functionality or modify existing.

Of course if you're trying to make something fairly simple, going with an MVC architecture could be overkill.

从此见与不见 2024-12-14 10:53:01

这都是偏好。我曾使用过 XTemplates 和 Smarty 等旧结构,现在已转向 Codeigniter 和 Kohona。我非常喜欢它们,它们非常适合我在网络上所做的一切。对于电话应用程序,我还可以为执行数据提取所需的功能设置控制器。在 Linux 世界和 Windows 世界中工作,对于构建 ASP.NET 网站,除了使用 MVC 之外,我没有看到其他构建网站的方法。 Visual Studio 中的 Web 应用程序项目仍在使用,但我不想再使用了。通过 Visual Studio 的 MVC 项目非常易于使用和设置。您可以右键单击控制器方法并自动创建视图。每种结构都有好有坏,但由开发人员决定使用满足其需求的结构。

It's all preference. I have worked with old structures like XTemplates and Smarty and have now moved on to Codeigniter and Kohona. I like them very much and they work very well for everything I do on the web. For phone applications I can set up controllers for the functions that are needed to do it's data pulls as well. Working both in the Linux world and Windows World, for building ASP.NET Web Sites I don't see other way of building websites beside using MVC. Web Applications Projects in Visual Studio are still used but I prefer not to anymore. MVC Projects via Visual Studio is so easy to use and set up. You can right click on your controller methods and create views automatically. In every structure there is a good and bad but it's up to the developer to use whatever meets their needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文