当前位置：首页　开发语言　正文

MVC is dead, it’s time to MOVE on.

2012年07月10日　|　作者: 米豆网 - 1000seo　|　分类: 开发语言　|　



I’m certainly not the first person to notice this, but the problem with MVC as given is that you end up stuffing too much code into your controllers, because you don’t know where else to put it.

To fix this I’ve been using a new pattern: MOVE. Models, Operations, Views, and Events.

MVC is dead, it's time to MOVE on.


Models encapsulate everything that your application knows.
Operations encapsulate everything that your application does.
Views mediate between your application and the user.
Events are used to join all these components together safely.
In order to avoid spaghetti code, it’s also worth noting that there are recommendations for what objects of each type are allowed to do. I’ve represented these as arrows on the diagram. For example, views are allowed to listen to events emitted by models, and operations are allowed to change models, but models should not refer to either views or operations.

The archetypal model is a “user” object. It has at the very least an email address, and probably also a name and a phone number.

In a MOVE application models only wrap knowledge. That means that, in addition to getters and setters, they might contain functions that let you check “is this the user’s password?”, but they don’t contain functions that let you save them to a database or upload them to an external API. That would be the job of an operation.

A common operation for applications is logging a user in. It’s actually two sub-operations composed together: first get the email address and password from the user, second load the “user” model from the database and check whether the password matches.



The login screen is a view which is responsible for showing a few text boxes to the user. When the user clicks the “login” button the view will yield a “loginAttempt” event which contains the username and password that the user typed.

Everything the user can see or interact with should be powered by a view. They not only display the state of your application in an understandable way, but also simplify the stream of incoming user interactions into meaningful events. Importantly views don’t change models directly, they simply emit events to operations, and wait for changes by listening to events emitted by the models.

The “loginAttempt” event is emitted by the view when the user clicks login. Additionally, when the login operation completes, the “currentUser” model will emit an event to notify your application that it has changed.


Why now?


To re-iterate: MVC is awesome, but it’s designed with decades old technologies. MOVE is just a update to make better use of the new tools we have.


本文章由 米豆网 - 1000seo 于2012年07月10日发布在开发语言分类下，您可以发表评论，并在保留原文地址及作者的情况下引用到你的网站或博客。
+复制链接 转载请注明：MVC is dead, it’s time to MOVE on.-米豆网