Introduction

This article gives an introduction to the use of Model-View-Controller (MVC) pattern with Adobe Flex. It illustrates the use of MVC on the Flex client-side as well as that of the Flex client as the View component in the MVC pattern.

Model-View-Controller architectural pattern increases the reusability of the various components and improves the maintainability of the overall system. The Flex application is generally considered to part of the View in a distributed MVC (Model 2 MVC). However, Flex implements its own classic MVC architecture at the client side with its own view, model and controller components.

MVC is the most used design pattern for the presentation layer in Flex. The view components for the client-side MVC are typically written in MXML. The model components, representing data at the client-side, are ActionScript objects. The client-side MVC in Flex also has controller components which are responsible for the communication with the back-end systems. The sections below discuss how using the MVC pattern enhances the Flex client, in the client-side MVC scenario, and the role the Flex client plays in server-side MVC scenario.

Client-side MVC

Figure 1 below shows an architecture diagram for the client-side MVC. As shown below, the MVC is a part of the flex presentation layer rather than in the Java layer.



Figure 2 shows the various components involved in a typical client-side MVC design. At the top level is the Flex application MXML file and Flex reusable components developed in MXML or ActionScript 2.0. Application MXML file holds all the views all the time but shows only one view at a time. This article does not deal with how this can be achieved.



All the user actions are redirected to a central Front Controller (which is an ActionScript 2.0 class). Front Controller holds a map of all the Command objects which know what exactly to do for a give user action.

Front Controller implementation in ActionScript 2.0 can be achieved with the collaboration of two key classes that work in an event-driven manner.
  • Controller - Configured to listen for specific events and delegate control to specific worker classes when these events occur
  • EventBroadcaster - Responsible for broadcasting events to the controller in response to user-gestures, changes in application state and so on.

Server-Side MVC

The MVC typically decouples the data access, the business logic and the data presentation. When building an RIA with a server implementing MVC, the Flex application can act as the 'View' part in MVC. This approach will mean that there is no impact on the model and the controller aspects of MVC architecture being used. Flex in this setup communicates with the using the traditional HTTP Request/Response mechanism. The following diagram illustrates this approach:



Comparing the two

Client-Side MVC using Flex and ActionScript Server-Side MVC using Struts
MVC layer in the client-side MVC layer in the server-side
Client side validation doesn't require round-trip to server over HTTP Client side validation requires round-trip to server over HTTP
Changing some of the views in the single Flash screen doesn't require round-trip to server over HTTP Can invoke all kinds of integration services in the Java layer like POJOs, EJB, and Web Services.
Changing a view in the single Flash screen requires round-trip to the server over HTTP Can invoke only POJOs from the Java layer (since all requests go to central controller in Java layer and controller is no more than a POJO)
Applications scale very well and the code is very neat. If round trips to the server are avoided (with the aim of building a scalable applications), the application cannot scale.

The most important distinction to note in the above comparison matrix is the integration of client-side MVC with server-side services is more flexible than that in the server-side MVC. Client-side MVC architecture has the privilege of integrating with any type of Java objects. In typical a J2EE application, Business services are made available to the presentation layer in many forms including simple POJOs, EJB and Web Services. So it's very important that the application architecture provides a single consistent platform for integration with backend services.

Server-side MVC kind of undermines the basic principle of Rich Internet Application. The basic principle of Rich Internet Application is to provide rich user experience. Rich user experience does not include the rich user interface alone, but also eye-blink response times for most of the user actions (if not all of them). So having a server-side MVC implies that all the user actions map to some object on the server side and the user actions are transmitted over HTTP to the server.

Summary

This article talked about the traditional MVC architecture and Flex's own classic MVC implementation on the client-side. The article also compared the two and discussed the advantages and disadvantages of integration a Flex application by following the two approaches.

References

  • http://www.Adobe.com/flex
  • http://www.theserverside.com/articles/article.tss?l=Flex
  • http://www.Adobe.com/devnet/flex/articles/struts.html
  • http://www.richinternetapps.com/archives/000017.html
  • http://www.Adobe.com/devnet/mx/flash/articles/ria_dataservices.html
  • http://www.Adobe.com/devnet/mx/flash/articles/databinding.html