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: Module->Core: Sharing instances vs. sharing classes across cores  (Read 32881 times)
openoli
Full Member
***
Posts: 44


View Profile Email
« on: August 25, 2016, 12:36:39 »

Hi,
this might be also already discussed in the past so sorry for asking again ;-)

In a lot of cases it's necessary to share e.g. login information across all cores.
I wonder if it's better to share a LoginProxy instance that resides in the shell or if it's better to share the LoginProxy class,
let each core create its own instance and just inject the LoginProxy data to the new instance.

[1] Share instance:
// Retrieve login proxy from shell and access login data directly
var shell:IFacade = Facade.getInstance(SharedConstants.SHELL);
var loginProxy:LoginProxy= shell.retrieveProxy(LoginProxy.NAME) as LoginProxy;
:
// Retrieve login proxy from shell and access login data directly
var shell:IFacade = Facade.getInstance(SharedConstants.SHELL);
var loginProxy:IProxy= shell.retrieveProxy(SharedConstants.LOGIN_PROXY) as IProxy;
var loginVO:LoginVO = loginProxy.getData() as LoginVO;


[2] Share class:
:
// Retrieve login proxy from shell, register new login proxy instance within this core and inject loginVO
var shell:IFacade = Facade.getInstance(SharedConstants.SHELL);
var loginProxy:LoginProxy = new LoginProxy(LoginProxy.NAME, shell.retrieveProxy(LoginProxy.NAME).getData() as LoginVO)
facade.registerProxy(loginProxy);
var loginVO:LoginVO = loginProxy.getData() as LoginVO;

Or would it be generally a bad idea to retrieve the shell from another core doing it this way?

Thanks in advance,
Olaf

« Last Edit: August 25, 2016, 09:51:39 by openoli » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: August 27, 2016, 07:19:23 »

If you package your model in a separate library, then each core can use it. You could make the LoginProxy have a class property that holds the ProfileVO. That would let you register it in every core, retrieve it, and be able to access the login info that was fetched by the instance in the shell. Make it 'protected static' and add a getter for it.

Cheers,
-=Cliff>
Logged
openoli
Full Member
***
Posts: 44


View Profile Email
« Reply #2 on: August 29, 2016, 07:31:05 »

Hi Cliff,
I've already outsourced my model for a long time but it never occurs to me to just introduce a static class var inside the proxy.
So, many thanks again for your support!

Cause this is probably a common use case I'd like to share the code:
:
public class LoginProxy extends Proxy
{
public static const NAME:String = "LoginProxy";

        // This could also be a VO of course
protected static var sharedAuthToken:String;

public function LoginProxy(proxyName:String=null, data:LoginVO=null) {
super(proxyName, data);
}

protected function setAuthToken(value:String):void {
LoginProxy.sharedAuthToken = value;
}

public function get authToken():String{
return LoginProxy.sharedAuthToken;
}
      
        ...
}

Thanks,
Olaf
« Last Edit: August 30, 2016, 02:16:56 by openoli » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #3 on: August 30, 2016, 11:03:16 »

Thats the ticket.  :)
Logged
Pages: [1]
Print