开发者

Service object return

Here is a little problem that I assume pops up ALOT in RIA's, Flex/AS3 especially. Allow me to explain, lets assume we have a complex object Car.

Car
{
   name:String;
   creationDate:Date;
   engineSize:String;
   pastOwners:Array;
   maintenenceOperations:Array
}

Now lets say my application updates a Car object by changing it's name and sends it out to the service. My question is how do you handle the returning of the object and updating the Car object present on the application side. Do you slog it and write the boilerplate for each class ? How do you handle if a property is lazy loaded ?

I have done it before writing the boilerplate update code, I just didn't want to go on reinventing the square wheel if there is a better way.

A request of an example of the update method was requested so here it is. It isn't complete with all aspects, but it captures the idea that you have an existing Car object, and you pass the new service object to it to rectify the differences.

[Bindable]
[RemoteClass("com.stackoverflow.example")]
public class Car
{
    public var name:String;
    public var engineSize:String;
    public var maintenenceOperations:Array;
    public var owner:Owner;

    public function update( value:Car ):void
    {
        if( value != null )
        {
            this.name = value.name; 
            this.engineSize = value.engineSize;  
            if( this.owner != null )
            {
                this.owner.update( value );
            }
      开发者_StackOverflow中文版  }
    }
}


Keep it simple, most of the time there is no need to handle the return of an object that was sent to the service to be updated.

If the object is modified by the service while it is been updated, get the result object and replace the hole flex object instead of checking its properties for change.


Short answer, you need some sort of ID (unique) that relates to the object. When the server returns, you can then look through an associative array (Dictionary) for that object id and update the appropriate properties.

Long answer, it really depends what you're trying to accomplish and how. There are many answers to this question, but it's up to you to find it which way is best for your specific case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜