<p>I've been utilizing the <a href="http://en.wikipedia.org/wiki/Command_pattern" rel="nofollow noreferrer">command pattern</a> in my Flex projects, with asynchronous callback routes required between:</p>

<ul>
<li>whoever instantiated a given command object and the command object,</li>
<li>the command object and the "data access" object (i.e. someone who handles the remote procedure calls over the network to the servers) that the command object calls.</li>
</ul>

<p>Each of these two callback routes has to be able to be a one-to-one relationship. This is due to the fact that I might have several instances of a given command class running the exact same job at the same time but with slightly different parameters, and I don't want their callbacks getting mixed up. Using events, the default way of handling asynchronicity in AS3, is thus pretty much out since they're inherently based on one-to-many relationships.</p>

<p>Currently I have done this using <strong>callback function references</strong> with specific kinds of signatures, but I was wondering <em>if someone knew of a better (or an alternative) way?</em></p>

<p>Here's an example to illustrate my current method:</p>

<ul>
<li>I might have a view object that spawns a <code>DeleteObjectCommand</code> instance due to some user action, passing references to two of its own private member functions (one for success, one for failure: let's say <code>"deleteObjectSuccessHandler()"</code> and <code>"deleteObjectFailureHandler()"</code> in this example) as callback function references to the command class's constructor.</li>
<li>Then the command object would repeat this pattern with its connection to the "data access" object.</li>
<li>When the RPC over the network has successfully been completed (or has failed), the appropriate callback functions are called, first by the "data access" object and then the command object, so that finally the view object that instantiated the operation in the first place gets notified by having its <code>deleteObjectSuccessHandler()</code> or <code>deleteObjectFailureHandler()</code> called.</li>
</ul>

[{'answer_id': 53743, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 1, 'selected': False, 'text': '<p>Many of the Flex RPC classes, like <code>RemoteObject</code>, <code>HTTPService</code>, etc. return <a href="http://livedocs.adobe.com/flex/2/langref/mx/rpc/AsyncToken.html" rel="nofollow noreferrer"><code>AsyncToken</code></a>s when you call them. It sounds like this is what you\'re after. Basically the <code>AsyncToken</code> encapsulates the pending call, making it possible to register callbacks (in the form of <a href="http://livedocs.adobe.com/flex/2/langref/mx/rpc/IResponder.html" rel="nofollow noreferrer"><code>IResponder</code></a> instances) to a specific call. </p>\n\n<p>In the case of <code>HTTPService</code>, when you call <code>send()</code> an <code>AsyncToken</code> is returned, and you can use this object to track the specific call, unlike the <code>ResultEvent.RESULT</code>, which gets triggered regardless of which call it is (and calls can easily come in in a different order than they were sent).</p>\n'}, {'answer_id': 53843, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 3, 'selected': True, 'text': '<p>I\'ll try one more idea:</p>\n\n<p>Have your Data Access Object return their own AsyncTokens (or some other objects that encapsulate a pending call), instead of the AsyncToken that comes from the RPC call. So, in the DAO it would look something like this (this is very sketchy code):</p>\n\n<pre><code>public function deleteThing( id : String ) : DeferredResponse {\n    var deferredResponse : DeferredResponse = new DeferredResponse();\n\n    var asyncToken : AsyncToken = theRemoteObject.deleteThing(id);\n\n    var result : Function = function( o : Object ) : void {\n        deferredResponse.notifyResultListeners(o);\n    }\n\n    var fault : Function = function( o : Object ) : void {\n        deferredResponse.notifyFaultListeners(o);\n    }\n\n    asyncToken.addResponder(new ClosureResponder(result, fault));\n\n    return localAsyncToken;\n}\n</code></pre>\n\n<p>The <code>DeferredResponse</code> and <code>ClosureResponder</code> classes don\'t exist, of course. Instead of inventing your own you could use <code>AsyncToken</code> instead of <code>DeferredResponse</code>, but the public version of <code>AsyncToken</code> doesn\'t seem to have any way of triggering the responders, so you would probably have to subclass it anyway. <code>ClosureResponder</code> is just an implementation of <code>IResponder</code> that can call a function on success or failure.</p>\n\n<p>Anyway, the way the code above does it\'s business is that it calls an RPC service, creates an object encapsulating the pending call, returns that object, and then when the RPC returns, one of the closures <code>result</code> or <code>fault</code> gets called, and since they still have references to the scope as it was when the RPC call was made, they can trigger the methods on the pending call/deferred response.</p>\n\n<p>In the command it would look something like this:</p>\n\n<pre><code>public function execute( ) : void {\n    var deferredResponse : DeferredResponse = dao.deleteThing("3");\n\n    deferredResponse.addEventListener(ResultEvent.RESULT, onResult);\n    deferredResponse.addEventListener(FaultEvent.FAULT,   onFault);\n}\n</code></pre>\n\n<p>or, you could repeat the pattern, having the <code>execute</code> method return a deferred response of its own that would get triggered when the deferred response that the command gets from the DAO is triggered.</p>\n\n<p>But. I don\'t think this is particularly pretty. You could probably do something nicer, less complex and less entangled by using one of the many application frameworks that exist to solve more or less exactly this kind of problem. My suggestion would be <a href="http://mate.asfusion.com" rel="nofollow noreferrer">Mate</a>.</p>\n'}, {'answer_id': 2824575, 'author': 'nsdevaraj', 'author_id': 339958, 'author_profile': 'https://Stackoverflow.com/users/339958', 'pm_score': 0, 'selected': False, 'text': '<p>The AbstractCollection is the best way to deal with Persistent Objects in Flex / AIR. The GenericDAO provides the answer.</p>\n\n<p>DAO is the Object which manages to perform CRUD Operation and other Common\nOperations to be done over a ValueObject ( known as Pojo in Java ).\nGenericDAO is a reusable DAO class which can be used generically.\nGoal:</p>\n\n<p>In JAVA IBM GenericDAO, to add a new DAO, the steps to be done is simply,\nAdd a valueobject (pojo).\nAdd a hbm.xml mapping file for the valueobject.\nAdd the 10-line Spring configuration file for the DAO.</p>\n\n<p>Similarly, in AS3 Project Swiz DAO. We want to attain a similar feet of achievement.</p>\n\n<p>Client Side GenericDAO model:\nAs we were working on a Client Side language, also we should be managing a persistent object Collection (for every valueObject) .\nUsage:\n\nSource: \n<a href="http://github.com/nsdevaraj/SwizDAO" rel="nofollow noreferrer">http://github.com/nsdevaraj/SwizDAO</a></p>\n'}]
['https://Stackoverflow.com/questions/53025', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4111/']