Thursday, January 13, 2011

Moving on to a new project

Since starting this blog, I have been assigned to a project which (as I try to remain diplomatic) has been a bit unsatisfying. Enough said about that - time to look to the future.




And what better way to celebrate than to take in the morning January sun at the town landing.

The ink is drying (if that metaphor can be used in this digital age) on the contract as I prepare to embark. Each project brings new challenges, and I look forward to applying the knowledge I gained from previous project challenges to solve new ones.

Before I leave, let me make note of a couple of things.

WSDL and ObjectProxy

This was a vexing problem that quite a few Flex developers have encountered, and it continues with Flex 4.

Here is the issue: You have a service method that is intended to return an collection of typed objects:

myService.getCars():Cars[]

If, as expected, a collection of 'Cars' is returned, you will see in Flex an array of typed objects. Now this assumes that you have generated all the valueObjects from the wsdl into your Flex project. If you don't know what I am talking, about see: Import WSDL into Flash Builder 4

The service above will return a collection of cars upon the getCars() call. You will undoubtedly code to expect this collection. But what happens if only one car is returned? No problem, you will get a collection with one typed object.

NOT!

The response will contain one 'Car' object, and it will not be in a collection.

And to make matters worse, that 'Car' will be wrapped in the Flex 'ObjectProxy' class, which will evaluate to 'null' if you try to inspect it's values.

There are two paths the follow here:
1) Create an object by importing mx.utils.object_proxy;

var obj:Object = resultEvent.result.object_proxy::object;

Now you will be able to inspect the properties of the object. However that object is not typed, and what you really want is a collection of 'Car' objects.

2) The best approach is to add the following line to the imported services. Again, refer to the link above, because this assumes you have correctly imported the wsdl.

You will note that importing a service will generate two classes, for example with the 'Car' object:

(a) CarManagerService.as
(b) _Super_CarManagerService.as

Inside the CarManagerService you will see the constructor:

function CarManagerService()
{
super();
}

Add the following snippet (import the Operation class):

function ConfigurationComponentManagerImplService()
{
super();
for each (var operation:Operation in operations)
{
operation.forcePartArrays = true;
}
}

Now, even if only one object is returned, it will be available in a collection AND it will be typed correctly.

No comments:

Post a Comment