Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Proxy and Business Delegate call  (Read 16248 times)
thomasvst
Courseware Beta
Jr. Member
***
Posts: 10


View Profile Email
« on: October 09, 2007, 05:52:28 »

Hi there,

if a Proxy has more than one interaction with a business delegate (in charge of asynchronous service call), is there a 'nice' way to handle the response / fault ? 

I mean that if a Proxy implements 'IResponder' interface, there is only one result / fault function in charge of handling the result. If I want the send a "UserDeleted" notification after a successful "removeUser()" call and a "UserAdded" notification after a successful "addUser()" call, what is the best solution ?

Is there a best practice in this case ? Should I implement a Command that implements 'IResponder' in charge of calling the BusinessDelegate and updating the Proxy after a successful call ?

Thanks a lot.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: October 09, 2007, 08:44:23 »

In PureMVC-land, we try and keep service communication within the realm of the Model, even though a Command could implement IResponder and talk to a service as well.

So you've got a Proxy that is an IResponder to a service that might've been called to do an update, delete, or add. It has only one onFault method and one onResult method. Depending upon what call was made, the onResult method must send a different Notification (i.e. 'userUpdated' or 'userRemoved'). But how do we 'remember' what it was we were doing?

The Asynchronous Completion Token (ACT) design pattern is the answer to the problem.

With Flex components like HTTPService, when you invoke the service, you get an instance of AsyncToken as the return. This is a dynamic class onto which you can add whatever properties you'd like. For instance, a reminder about what call we just made:

:

var myToken:Object =  myHTTPService.send();
myToken.myOperation= "updateUser";


Then later, when the service returns, and the onResult method is passed the event, just check the 'event.token' property, and you'll find it is a reference to the token that you got when you made the call.

:

public function onResult(event:ResultEvent):void
{
    switch ( event.token.myOperation ) {
           case 'updateUser': // set the returned data and alert the app
                 data = event.result.user;
                 sendNotification('userUpdated', event.result.user);
                 break;

           case 'removeUser':  // we also kept the old user ID on the token this time
                 sendNotification('userRemoved', event.token.myOperation.removedUserID);
                 break;         
    }
}


-=Cliff>
Logged
thomasvst
Courseware Beta
Jr. Member
***
Posts: 10


View Profile Email
« Reply #2 on: October 09, 2007, 08:50:34 »

Thank you.
Logged
immerzeel
Courseware Beta
Jr. Member
***
Posts: 14


View Profile Email
« Reply #3 on: November 19, 2007, 07:24:16 »

Does the token also apply to Remoting calls?

I would like to handle my remoting in a switch function as well.
Logged
thomasvst
Courseware Beta
Jr. Member
***
Posts: 10


View Profile Email
« Reply #4 on: November 19, 2007, 08:01:40 »

Yes, it works.
Logged
robcos
Newbie
*
Posts: 1


View Profile Email
« Reply #5 on: April 21, 2008, 06:33:31 »

:

var myToken:Object =  myHTTPService.send();
myToken.myOperation= "updateUser";


Isn't this non thread-safe? If the services answers very fast the "myOperation" variable may not have been set.
I know it maybe not very probable but should we use a theorically bad pattern?
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #6 on: April 21, 2008, 06:56:43 »

Flash is single-threaded.

-=Cliff>
Logged
Pages: [1]
Print