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: Composite data structures: Advice needed for creating the model structure  (Read 8125 times)
olist
Newbie
*
Posts: 7


View Profile Email
« on: February 27, 2009, 03:43:09 »

Hi everyone,
I'm working on a AS3 game using PureMVC and got literally stuck on the middle of the road. Any help or advice is highly appreciated, maybe I'm completely on the wrong track...

On the model side I have a number of drivers and each driver has a list of cars he has created before. Drivers and cars can appear in different views at the same time.

I have separate VOs for cars and drivers and service delegates for server communication. When requesting a driver, I only know its unique ID. The service responds some attributes and a list of assigned car IDs. After parsing the data I request lightweight instances of the referenced cars based on their IDs.

At the moment, every driver VO is assigned to its own DriverProxy instance, car VOs are accessed via CarProxy instances respectively. I access those proxies via the unique driver and car IDs like this:

:
var proxy:CarProxy = facade.retrieveProxy(car.id) as CarProxy;
I'm stuck in the question of how to model the relationship between a driver and its cars:
  • Should the driver VO have a list of car IDs? Or a list of car VOs? Or nothing? Since the driver VOs should be easy to serialize, I tend to the first option, because sending a list of car VOs to the server when serializing a driver makes no sense in my opinion, no? Or maybe the driver VO shouldn't know anything about the cars at all? I have taken a look at the EmployeeAdmin example where the RoleProxy handles the relationship between *all* known users and their roles, while at the same time the UserVO doesn't know about the user's roles. The relationship may only exist in the proxy in this case and be used to provide methods like
    DriverProxy.getCars():Vector.<CarModel>;
  • When parsing the driver data that I received from the server, I come to the point where I find the assigned car IDs. Parsing the data is done in DriverProxy. Based on the car ID I could create a barebone car instance and its CarProxy. Via this proxy I am able to request further car details. But is it okay to create and register a proxy from within another proxy? Feels odd...
  • If I created the CarProxy instance like described above, who is responsible for removing the CarProxy once its no longer needed? The actor who creates and registers a proxy with the facade should also take care of tearing it down once its no longer needed, no? I'm happy with that as long as the car's lifecycle ends when the driver dies too, but what if the car and its proxy are still visible in another context? Say the driver is removed from the stage and therefore its proxy gets removed from the facade, but the driver's car is part of a highscore table thats still visible? If the car's proxy had been torn down, it would no longer be available for the highscore mediator. Do I need an additional proxy that keeps track of the number of references to a car and that handles creating and destroying the proxies?

I hope I explained the problem clearly enough, in my head things are getting quite mixed up.
Thanks for your advice, Cheers, Oliver
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: March 01, 2009, 08:27:58 »

I think it might be easier if you had one CarsProxy, which held an array of cars. It could have add, get and remove car methods. When a driver is retrieved, you could request all the associated cars in one call keeping the result in one proxy. So you avoid the hassle of deciding when and how to remove unneeded proxies.

Sticking with your existing setup, you need to know when a given Proxy is no longer needed, when claims to it come from actors that don't know each other. So you could add a usage count to the proxy and whenever something retrieves it, bump up the count and when done, decrement it. When its decremented to zero, go ahead and have the Proxy remove itself.

There is a utility called PGProxyReservation listed in the 'Public Apps, Demos and Utilities' area of these forums that I believe is geared to this sort of scenario. You might want to scope that out before implementing.

-=Cliff>
Logged
Joel Hooks
Courseware Beta
Sr. Member
***
Posts: 146


baby steps

 - 46288188  - passport@provinsal.com  - joeltuff
View Profile WWW Email
« Reply #2 on: March 01, 2009, 05:29:01 »

"There is a utility called PGProxyReservation listed in the 'Public Apps, Demos and Utilities' area of these forums that I believe is geared to this sort of scenario. You might want to scope that out before implementing."

This utility works really well after a few tweaks to handle dynamically named proxies.

http://forums.puremvc.org/index.php?topic=830.0

I've used it in production on multiple projects.
Logged

http://joelhooks.com - my ramblings about developing with actionscript and python using pureMVC and django respectively.
olist
Newbie
*
Posts: 7


View Profile Email
« Reply #3 on: March 03, 2009, 03:55:28 »

Thanks to Cliff and Joel for the advice. After wracking my head during the weekend I decided to go for the one-proxy solution and did some refactoring. So now I'm glad to see that you would do it the same way.
Cheers, Oliver
Logged
Pages: [1]
Print