开发者

Architecture more suitable for web apps than MVC?

开发者 https://www.devze.com 2023-04-10 03:57 出处:网络
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 s

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?


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


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!


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消