Wednesday, August 5, 2009

Flex and Pure MVC

I learned PureMVC for Flex about a month ago, and was able to successfully migrate all of my Flex projects at work to this new framework. Of course, these projects are relatively small (appx 5000 lines of code at best) but it was the best move I could have made.

Though PureMVC doesn't necessarily guarantee good design and separation of concerns in a project, it certainly provides a nicely decoupled way of utilizing the MVC design scheme.

PureMVC uses what's called a Facade to act as a broker for forwarding notifications from various components. View components (MXML and the ActionScript needed to drive them) are provided to "Mediators" which communicate via the Facade. These mediators translate any events from the UI (e.g. users clicking buttons to load information from a server) into "notifications" which are sent via the Facade to the respective Controller objects.

These Controller objects are known as "Commands". Commands receive the notification from the Facade (and any data passed from the View components) and then use "Proxies" to request data and perform the majority of the business logic for the application.

Sound complicated? It's really not. The typical project may be organized like this:

src
|- controller
|- model
|_ view

Under the "controller" directory, Commands are kept which receive notifications. These commands invoke "Proxies" which are stored in the "model" directory. These proxies request information from remote objects (e.g. servers) and relay data back to the appropriate location. The "view" folder contains two different types of objects: view components and mediators. The view components are simply the user-visible forms and controls in which the user interacts. These have absolutely no connection to any PureMVC components; rather, they are passed (at runtime) to a "Mediator". The mediator listens for events from the view components and then relays the event to the Facade as a notification.

Frankly, it sounds a lot more complicated than it is. Future posts should have some examples for how to assemble a simple User-form in PureMVC

No comments: