topleft
topright

OReilly PureMVC Book

PureMVC Book

PureMVC TV

PureMVC TV

PureMVC on Google

PureMVC Blog-o-Sphere

PureMVC on Linked-In

PureMVC on LinkedIn

Supporters of PureMVC

 
Image
 
Why not use Commands as Async Responders?
Wednesday, 09 July 2008

QUESTION: 

 

As the Command is instantiated and executed locally in a controller function, theoretically the Command object is immediately available for garbage collection when the method finished executing, right?

 

Now what if you setup asynchronous event listeners in the Command's execute function?

 

Everything was fine in my case and events were fired and executed correctly but I nevertheless felt a bit unsure about it... quite a few things have changed in AS3 with GC and one can easily run into a situation where listeners aren't executed because the source already has been collected.

 

ANSWER:

  • Within a PureMVC application, there are a couple of reasons:

 

1. Commands should be stateless.

 
They are the object-oriented equivalent of a subroutine - a little place for us to break down to some procedural code, which is by definition synchronous in nature. By making a running Command a responder to an async process like a service call, you have given it state. It is in a waiting state. If it has a long timeout and the user clicks a 'stop' button, how do you interrupt it? You can't. You can have another Command tell the delegate to cancel the HTTPService call, but does that free the reference to this Command, which was on the responder list for the call?

You have handed a reference to a Command to another framework that you don't control. You have no way to ensure that that reference will eventually resolve to a result or fault callback (and that the framework has cleaned up its reference before returning) that will collapse scope and make the Command go away. Also, remember this pattern is implemented on multiple platforms, so any assumptions about the how the reference will be resolved by other framework (Flex, J2ME, etc) may not hold universally, so the practice must be one that can be advocated on any platform..

Meanwhile a Proxy is a registered, long-living actor and the framework has a method for removing it or retrieving it (for instance to tell it to cancel an operation). The framework can only remove a mapping for a Command. This is why we only have the Controller create and execute Commands and we avoid giving them state so they get their work done and go away, and we control their scope.

2. This is Domain Logic not Business Logic.

 
There is a distinction between the two. Business logic is concerned with coordinating valid interactions between View and Model. Domain Logic is about keeping an internally consistent Model representation on both sides of the app (client and server).

The Proxy's job is to hide the location of the data, and make it irrelevant to the rest of the application. The View and Controller regions need only know that a particular Proxy wants to be communicated with in a synchronous fashion or async.

This is why services have been entirely relegated to the Model region. You can write a more portable Model this way.

Several applications with different use cases and View/Controller arrangements may employ the same Model classes without having to reimplement or copy the code for keeping the Model internally consistent. 

 

-=Cliff>

Delicious
Technorati
Reddit
Furl it!
NewsVine
YahooMyWeb
Stumble
blogmarks
Digg
co.mments
connotea
 

PureMVC on the Web

João Pescada has produced a nice PureMVC / AS3 demo - a searchable image gallery. 
 
Photo Search with PureMVC - Article  -  Code  -  Demo

 

 
Jonathan Campos has put together a nice introductory PureMVC article with an example built around the Netflix API.
 
Netflix API with PureMVC Article

 
The guys over at Pyjamas (a Python port of GWT) have made their compiler so efficient that it can take virtually any Python library and it will compile to Javascript. They loved the look of PureMVC Python, and decided to do a couple of demos that use it.
 

Pyjamas Project

• EmployeeAdmin Demo: Original / Pyjamas  

Pyjamas PureMVC Timesheet Demo

 
Chandima Cumaranatunge, co-author of the O'Rielly book AS3 Design Patterns, and long-time PureMVC community member recently created a simple Flash/AS3 MultiCore demonstration application accompanied by an informative article that's an excellent introduction to the benefits of working with MultiCore.
 
Who Moved the Cheese? - Demo / Source / Article

ActionScript 3 Design Patterns

 

Ahmed Nuaman has published a well conceived and received AS3/PureMVC tutorial at FlashTuts.com. It's a good place for the learner to get their feet wet, and goes into the difference between using PureMVC with Flex as opposed to Flash or pure AS3.

 

Understanding the PureMVC Open Source Framework

FlashTuts Website
Ahmed's Website

 

 

Recent Project Activity

PureMVC Standard Version for Perl 1.0  has been released, complete with unit tests and online documentation.

 

PureMVC Standard Framework for Perl

 

Release 1.3 of the AIR DesktopCitizen Utility which allows applications to remember their window size, position and maximized/minimized state each time it is launched. It also provides a facility for requiring confirmation (or executing some shutdown process) before the application window closes.

 

AS3 / AIR Desktop Citizen Utility

CodePeek Demo

 

The classic PureMVC EmployeeAdmin Demo has been ported to yet another language! Although PHP is usually thought of as a server language, using PHP-GTK, Sasa Tarbuk recreates th a desktop app. 

 

 
Version 1.0.8 of the PureMVC Java MultiCore port is now available. It supports JavaFX and handles threading properly.

 

 

The PureMVC Java MulitCore GWT EmployeeAdmin Mobile Demo has been released. Using Google Web Toolkit 2.0, it runs on iPhone, iPad and Android. 

 

 
Copyright © 2006-2008 Futurescale, Inc.