MVC is a phenomenal idea. You have models, which are nice self-contained bits of state, views which are nice self-contained bits of UI, and controllers which are nice self-contained bits of …
What?
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.
Overview
I'll define the details in a minute, but this diagram shows the basic structure of a MOVE application.
- 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.
Models
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.
Operations
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.
Operations are the doers of the MOVE world. They are responsible for making changes to your models, for showing the right views at the right time, and for responding to events triggered by user interactions. In a well factored application, each sub-operation can be run independently of its parent; which is why in the diagram events flow upwards, and changes are pushed downwards.
What's exciting about using operations in this way is that your entire application can itself be treated as an operation that starts when the program boots. It spawns as many sub-operations as it needs, where each concurrently existing sub-operation is run in parallel, and exits the program when they are all complete.
Views
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.
Events
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.
Listening on events is what gives MOVE (and MVC) the inversion of control that you need to allow models to update views without the models being directly aware of which views they are updating. This is a powerful abstraction technique, allowing components to be coupled together without interfering with each other.
Why now?
I don't wish to be misunderstood as implying that MVC is bad; it truly has been an incredibly successful way to structure large applications for the last few decades. Since it was invented however, new programming techniques have become popular. Without closures (or anonymous blocks) event binding can be very tedious; and without deferrables (also known as deferreds or promises) the idea of treating individual operations as objects in their own right doesn't make much sense.
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.
P.S. I'm not the only one beginning to think this way either, if you like the idea of MOVE you should check out
objectify and
interactions which try to add some of the benefits of MOVE to existing MVC applications. Please
let me know if you have other links that should be here!
P.P.S This blog post has been translated into Japanese no fewer than twice:
d.hatena.ne.jp and
blog.neo.jp. Thanks!
分享到:
相关推荐
From here, we move on to controllers, views, validations, Spring Tag libraries, and more. Finally, we integrate it all together to develop a web application. You’ll also get to grips with testing ...
This book takes a test-first approach to the main features of ASP.NET MVC. You’ll get hands-on experience building and deploying a time-management web application from start to finish. From building ...
Best-selling author Adam Freeman has thoroughly revised this market-leading book and explains how to get the most from ASP.NET Core MVC. He starts with the nuts-and-bolts and shows you everything ...
The ASP.NET MVC 5 Framework is the latest evolution of Microsoft’s ASP.NET web platform. It provides a high-productivity programming model that promotes cleaner code architecture, test-driven ...
Ext.NET.MVC.Pro.2.2.0.MVC.Examples.Explorer.zip 是一个包含Ext.NET MVC专业版2.2.0的示例集合,专为ASP.NET MVC开发者设计。这个压缩包提供了丰富的控件演示,帮助用户理解并掌握如何在实际项目中应用这些功能...
c# MVC 基础引用DLL
installing the ASP.NET MVC 2 Release Candidate release on a computer where Visual Studio 2010 Beta 2 is also installed is not supported. Documentation Documentation for ASP.NET MVC 2, which ...
You will implement Hypermedia and HATEOAS to guide your customer's stateless conversation with the product and see how a messaging-service based on WebSocket can be configured. Finally you will learn...
The ASP.NET MVC 5 Framework is the latest evolution of Microsoft’s ASP.NET web platform. It provides a high-productivity programming model that promotes cleaner code architecture, test-driven ...
Bring dynamic server-side web content and responsive web design together to build websites that work and display well on any resolution, desktop or mobile. With this practical book, you’ll learn how ...
The ASP.NET MVC 5 Framework is the latest evolution of Microsoft’s ASP.NET web platform. It provides a high-productivity programming model that promotes cleaner code architecture, test-driven ...
which was all there was to ASP.NET before the MVC Framework came into existence, is in an undeniable decline. While Microsoft is continuing to support and improve Web Forms, the momentum is clearly ...
This book is a great companion for beginners who want to learn Spring MVC. With the help of real-world examples, you will learn how to create a completely functional web application. You will also get...
CVE-2022-22963 复现Demo,A Spring MVC or Spring WebFlux application running on JDK 9+ may be vulnerable to remote code execution (RCE) via data binding. The specific exploit requires the application ...
ASP.NET Core MVC is a web application development framework from Microsoft that combines the effectiveness and tidiness of model-view-controller (MVC) architecture, ideas and techniques from agile ...
7 Data Entry in ASP.NET MVC.............................. 317 8 The ASP.NET MVC Infrastructure ......................... 355 9 AJAX Capabilities ....................................... 401 10 ...