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: How to handle/manipulate (hierachical) VO's and remote proxies  (Read 7374 times)
openoli
Full Member
***
Posts: 44


View Profile Email
« on: January 15, 2010, 08:00:48 »

Hi,
I've some general questions regarding handling VO's and remote proxies:

1)
VO's are used for server side communication and the VO's represents the data model, right ?
I've often read that VO's should contain only properties, no methods. Is it still a good way to implement also some methods to manipulate the VO itself at client side ?

2)
Is it the right way to pass a reference of an VO to the proxy data element ?

3)
Should the proxy implements some methods to manipulate the passed VO or should we use the
methods inside the VO to do this (see question 1) ... or should we use an additional "model class" ?

4)
What could we do, if we'd like to make the application able to work offline ?
Is one way to implement an additional "offline proxy" and switch between offline and online modus (proxy) ?

Hmm... seems that I'm not really able to formulate my questions in an understanding way...
perhaps you can provide some well desigend sample code for the example below ?

Many thanks in advance !
Olaf

Example:
Imagine we've a basket (BasketVO) that's able to pick up some items (ItemVO).
Inside the BasketVO we have a method "addItem()" to add an new item to the basket.
We'd like to save a new item to the remote server's database directly after e.g. clicking an "add item" button by using the "AddItemCommand".
(Following code is only an example to clarify my questions)

:

// BasketVO
public class BasketVO
{
public var userId:String;
public var items:ArrayCollection;

public function BasketVO (userId:String):void
{
              this.userId = userId;
}
 
        // adds new item to the basket
        public function addItem(item:ItemVO):void
        {
                this.items.addItem(item);
        }

}

// ItemVO
public class ItemVO
{
public var itemNo:int;
public var price:Number;
        public var count:int;

public function ItemVO ():void
{
}

        // is it a good idea to place this kind of method here ?
        public function calcSum():Number
        {
                return count*price;
        }
}

// the proxy
public class BasketProxy extends Proxy implements IProxy
{
       public static const NAME:String = 'BasketProxy';
     
       private var _basketService:RemoteObject;
       
       public function BasketProxy(userId:String)
       {
            super( NAME, new BasketVO(userId) );

            // set remote service
            _basketService = new RemoteObject();
            // ...

            _basketService.addItem.addEventListener(ResultEvent.RESULT, onAddItem);

       }

       public function get basketVO():BasketVO
       {
             return data as BasketVO;
       }

       // calls remote service to add a new item
       public function addItem(itemVO:ItemVO):void
       {
           // add item to remote basket and save item into DB
            _basketService.addItem(itemVO);
       }

       public function onAddItem(event:ResultEvent):
       {
           var returnedItem:ItemVO = event.result as ItemVO;
 
           // add item to local basket
           // is this the right way ???
           basketVO.addItem(returnedItem);

          // send notification
          // ...           
       }
}

// command that's add a new item
public class AddItemCommand extends SimpleCommand
{
override public function execute(note: INotification ) :void
{
            var itemVO:ItemVO = note.getBody() as ItemVO;

            var basketProxy:BasketProxy = facade.retrieveProxy("BasketProxy") as BasketProxy;;

            basketProxy.addItem(itemVO);
}
}


« Last Edit: January 15, 2010, 08:03:33 by openoli » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: January 16, 2010, 02:37:44 »

Value Objects are not only for crossing the boundaries between the client and server, but also across the internal tiers of the client and of the server. Even if an application doesn't have a server component, it is a good practice to use Value Objects to pass complex information structures between the Model, View and Controller tiers.

As for whether to use methods, the number one reason for it is to provide derived values. For instance, you might have a firstName field and a lastName field. Then you might have a name getter that joins the two.

Another thing you can do with Value Objects is to wrap an XML structure. If your client and server exchange XML, the client can create a 'Smart VO' (tm) that exposes the parts of the structure as properties. See an example of a 'Smart VO' here: http://forums.puremvc.org/index.php?topic=1293.msg5973#msg5973

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


View Profile Email
« Reply #2 on: January 19, 2010, 07:01:32 »

hey cliff,
thanks... it becomes a bit more clear for myself !
the 'smart vo' sounds interesting.

olaf
Logged
Pages: [1]
Print