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: RemoteObject as Singleton instance  (Read 9897 times)
dancantong
Jr. Member
**
Posts: 12


View Profile Email
« on: March 03, 2010, 02:12:39 »

I have several proxys in my application that invoke to some remote objects. Different proxys can call to same operations in remote objects. I have defined a singleton wrapper service object which holds an instance of RemoteObject. The service exposes a getOperation() method that returns remote object operations. Proxys obtain references to the singleton service to obtain remote object operations. I've noticed that Flex RemoteObject.getOperation() method returns always the same operation object reference from RemoteObject, so new result and fault event listeners are added to existing ones. Then problem is multiple event listeners for same operation are been fired when operation is invoked, resulting in different proxy event listeners been executed. I only need one event listener to be fired (the one who is related to operation invoked in the proxy).

My remote service looks like:

:
private class RemoteService
{
                protected static var _remoteService:Service = new RemoteService();
               
protected var _destination:String;

protected var _remoteObject:RemoteObject;

public function RemoteService(destination:String)
{
this._destination = destination;
this._remoteObject = new RemoteObject();
this._remoteObject.destination = destination;
}
           
                public static function getInstance():RemoteService
                {
                        return _remoteService;
                }

public function getOperation(operationName:String, onResult:Function, onError:Function) : Operation {
var operation:Operation = _remoteObject.getOperation(operationName) as Operation;
operation.addEventListener(ResultEvent.RESULT, onResult);
operation.addEventListener(FaultEvent.FAULT, onError);
return operation;
}
}

An example of a proxy:

:
public class TestProxy
{
public static const NAME:String = "TestProxy";

                protected var _remoteService:RemoteService;
 
public function TestProxy(){
super(NAME);
                        _remoteService = RemoteService.getInstance();
}

public function testOperation():void
{
var operation:Operation = _remoteService.getOperation("testOperationName", onTestResult, onTestFault);
operation.send();
}

                ...


Many proxys could have a reference to same RemoteService so when invoking testOperationName operation every event listener will be fired in different proxys.

I need a work around of this issue. I've thought of defining RemoteService as a Multiton instead of a Singleton, having one instance for each proxy. The I could have  _remoteService = RemoteService.getInstance(PROXY_NAME) in each proxy. As each RemoteService has a reference to a diffent RemoteObject, operations will be unique too for each Proxy.
But then I will have many RemoteObject references (one for each proxy), with consecuent memory consumption (now I have only one RemoteObject reference).

Is this approach acceptable?

Thank you very much.
Logged
dancantong
Jr. Member
**
Posts: 12


View Profile Email
« Reply #1 on: March 03, 2010, 03:55:49 »

I think another option would be using AsyncTokens instead of eventListeners. This way responders would be set up in each operation invocation, although I'm not really sure if previous responders would be eliminated when setting a new one. Then I could have only one RemoteObject instance shared between every Proxy. But this approach involves modifying more application code than Multiton so I would prefer first one.

Any comment on this will be appreciated!

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



View Profile WWW Email
« Reply #2 on: March 03, 2010, 08:23:29 »

You're going to be better off having each proxy instantiate it's own service object internally. It will be more straightforward than trying to share a 'global' one and manage its listeners.

-=Cliff>
Logged
dancantong
Jr. Member
**
Posts: 12


View Profile Email
« Reply #3 on: March 03, 2010, 09:10:01 »

Ok, so the Multiton Service approach seems good enough.

Thank you Cliff.
Logged
jpwrunyan
Sr. Member
****
Posts: 84


View Profile WWW Email
« Reply #4 on: March 03, 2010, 05:50:22 »

Ok, so the Multiton Service approach seems good enough.

Thank you Cliff.
If I may ask, why do you find the Singleton/Multiton approach so appealing?  It seems to only have made things difficult for you.  If each proxy just contains an internal instance of whatever RemoteObject it needs to use to get the data it wraps, wouldn't that achieve what you want with a simpler implementation?  Seems that would obviate your listener issues...
Furthermore, if each Proxy has its own key to get an instance from the Multiton, how is that different in the end from just have a RemoteObject instance variable inside each Proxy class?

The only difference I can see is how memory might be used in the application.  In which case I would actually prefer the internal instance over the Multiton because I could clear it from memory easily.

Anyway, apologies if I misunderstood what you're trying to do.
Logged
dancantong
Jr. Member
**
Posts: 12


View Profile Email
« Reply #5 on: March 04, 2010, 02:36:19 »

Well, is a matter of reusability and configuration. With this approach I avoid having to configure RemoteObject destination, timeout, etc. in each proxy. That is what a service/delegator offers.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #6 on: March 04, 2010, 09:17:58 »

You can certainly create a Delegate that provides the service instance, its config and exposes the methods for calling the service. Then have each Proxy instantiate a delegate internally. You can see this in use in the Cafe Townsend demo1. In that demo, the EmployeeProxy instantiates a LoadEmployeesDelegate that handles the service for the Proxy.

For Cafe Townsend, the only Proxy calling the service was the EmployeeProxy, so it's a bit of overkill to have the Delegate, the Proxy could've managed the service just fine, nothing else in the app needs access to it.

But in your case, since you have multiple Proxies needing access to the same service methods, it makes sense to follow this pattern. You're still going to have multiple instances of the service, one for each Proxy/Delegate but you won't have any issues tracking which Proxy made which call.

-=Cliff>

1PureMVC Cafe Townsend Demo:
http://darkstar.puremvc.org/content_header.html?url=http://puremvc.org/pages/demos/AS3/Demo_AS3_Flex_CafeTownsend/srcview&desc=PureMVC%20Source:%20Cafe%20Townsend%20Demo%20for%20AS3


Logged
jpwrunyan
Sr. Member
****
Posts: 84


View Profile WWW Email
« Reply #7 on: March 04, 2010, 05:42:43 »

Well, is a matter of reusability and configuration. With this approach I avoid having to configure RemoteObject destination, timeout, etc. in each proxy. That is what a service/delegator offers.

Thanks.  After reading your and Cliff's responses, I see what your issue is now.  (I haven't actually used RemoteObject since Flex 1.5 and I don't think we had Delegator classes in that project)
Logged
Pages: [1]
Print