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: Using an External API with PureMVC  (Read 4266 times)
Nebulous
Newbie
*
Posts: 3


View Profile Email
« on: April 01, 2011, 12:24:57 »

Hi,

I have just started using this framework, and so far I like how structured it is.

The only real problem I am having currently is how to integrate our GameAPI into it correctly... I understand that it should be a proxy, as it is retrieving and sending data? It uses simplexml to parse and process XML responses....

So I need work out the best way to...

1) Create a global object that can be accessed via anywhere
2) Make sure this object is only instaniated once
3) Post data to the API which then parses it into XML and sends it to our server
4) Handle the responses and callbacks of the API.

I spent a while looking at this yesterday, but could not figure out the correct implementation of how to do it.

Any help would be great.

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



View Profile WWW Email
« Reply #1 on: April 01, 2011, 10:00:51 »

1) Create a global object that can be accessed via anywhere
This is an antipattern that MVC is meant to correct. If you want a 'global object' you can just make a class with static methods and properties. You don't need a framework for that. But as soon as you do that then classes all over the app begin to depend on this object and it becomes 'the place to put things'. You end up with a Big Ball of Mud[1]

Make sure this object is only instaniated once
You don't have to implement a singleton, you have that in the Model. It's a registry. When you register a Proxy with the Model, only one instance can be stored with that name. That's why we use the NAME constant to register and retrieve Proxies. The nice thing is that using the same pattern, you can register a Proxy that you do want multiple instances of by using a unique name for each.

Post data to the API which then parses it into XML and sends it to our server
Expose a method on the proxy that takes the XML and sends it to the server. The Proxy can implement the Remote Proxy pattern.

Say you've generated some data at the UI, you dispatch an event heard by the Mediator for the component that generated the data, who handles the event by plucking the data off its view component, sends it off in a notification that triggers a Command that parses the data into XML and invokes the Proxy's method with the XML, triggering the server call.

Or if you use a SmartVO[2], it will manage the conversion of the data between XML and typed object, so you don't have to have that parsing happen in a Command. In which case, you can simplify the above example by having the Mediator pluck the SmartVO off the view component and immediately invoke the Proxy method.

And finally, if you don't want to use a Command or a SmartVO, you can consider the typed-to-XML conversion process to be 'Domain Logic' and put it in the Proxy method. So that the Proxy takes a typed object turns it into XML and makes the call.

Handle the responses and callbacks of the API.
The Proxy makes the call and listens for the response. When it gets the response, you can send off a notification that a Command and or Mediators may act upon.

-=Cliff>

[1] Big Ball of Mud - http://www.laputan.org/mud/mud.html#Abstract
[2] See an example of a 'Smart VO' here: http://forums.puremvc.org/index.php?topic=1293.msg5973#msg5973
Logged
Nebulous
Newbie
*
Posts: 3


View Profile Email
« Reply #2 on: April 04, 2011, 03:45:18 »

Cliff,

Thank you very much for your detailed response.

I am glad I waited for the reply, rather then hacking it in :)

I shall give it a try shortly, and if I get stuck I shall let you know. It doesn't help that the API i'm trying to implement is a great big ball of mud :(

Thanks again..

Dave.
Logged
Pages: [1]
Print