Use Custom Value Objects with Web Services in Flex 3 Beta 2 !

Flex 3 Beta 3 has some great new features that have been very well promoted like the new WSDL import tool and data wizard. But there’s also a wonderful hidden gem that greatly improves interoperability with Web Services–SchemaTypeRegistry. This class allows you to communicate with a server via web services and still retrieve data as your own custom value objects.

In the old days (last week), you could have a Student class on client and server, send Student from client to server, but when sending it back, you retrieved only Object. No more! Now you can send custom classes both ways.

SchemaTypeRegistry works a lot like the RemoteClass meta-tag for AMF remoting, but instead of taking simply a remote name it uses a QName–combination of Namespace and Local Name. The following example shows how to retrieve a custom Student class from a .NET web service, but the client code would be the same for any SOAP-based web service.

We start with our Student class definition which must have the same property names as those defined on the server class:

package 
{ 
  public class Student 
  { 
    private var _firstName:String; 
    public function get FirstName():String { 
      return _firstName; 
    } 
    public function set FirstName(value:String):void { 
      _firstName = value; 
    } 
 
    public var _lastName:String; 
    public function get LastName():String { 
      return _lastName; 
    } 
    public function set LastName(value:String):void { 
      _lastName = value; 
    } 
 
    public var _gpa:Number; 
    public function get Gpa():Number { 
      return _gpa; 
    } 
    public function set Gpa(value:Number):void { 
      _gpa = value; 
    } 
  } 
}

Then we’ll create a very simple sample application that shows what happens when we retrieve this class from a server, both with and without SchemaTypeRegistry.

<!--l version="1.0" encoding="utf-8--> 
<application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"> 
  <script> 
    <!--DATA[ <-->      import mx.rpc.xml.SchemaTypeRegistry; 
      import mx.utils.ObjectUtil; 
 
      private function registerVO():void { 
        var qn:QName = new QName("http://labs.atellis.com/ns/ws/", "Student"); 
        SchemaTypeRegistry.registerClass(qn, Student); 
        dump.text += "Registered QName " + qn + "nn"; 
      } 
 
      private function unregisterVO():void { 
        var qn:QName = new QName("http://labs.atellis.com/ns/ws/", "Student"); 
        SchemaTypeRegistry.unregisterClass(qn); 
        dump.text += "Unregistered QName " + qn + "nn"; 
      } 
 
    ]]&gt; 
  </script> 
 
  <webservice wsdl="http://localhost/web2/CustomVO.asmx?wsdl" id="ws"> 
    <operation result="dump.text += ObjectUtil.toString(event.result) + 'nn'" name="GetStudent"> 
 
  <hbox> 
    <button label="Get Student" click="ws.GetStudent()"> 
    <button label="Register VO" click="registerVO()"> 
    <button label="Unregister VO" click="unregisterVO()"> 
 
  <textarea height="100%" fontFamily="Courier New" width="100%" id="dump">When we run sample we can click "Get Student" and it will dump out the result. Notice the return is typed "Object". Then click "Register VO" and "Get Student" and now the return type is "Student". This demonstrates that the web service returned a typed object.
 
<img src="http://labs.atellis.com/wp-content/uploads/2007/10/schematyperegistry_screenshot.png" alt="SchemaTypeRegistry Screen Shot" />
 
One thing that you may be wondering is where exactly does the QName come from? Well, it's in the WSDL.
 
<img src="http://labs.atellis.com/wp-content/uploads/2007/10/schematyperegistry_wsdl.png" alt="SchemaTypeRegistry WSDL" />
 
To complete the example, here is the C# code used for the web service.
<pre lang="csharp">
using System.Web.Services; 
 
[WebService(Namespace = "http://labs.atellis.com/ns/ws/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
public class CustomVO : WebService { 
 
  [WebMethod] 
  public Student GetStudent() { 
    Student s = new Student(); 
    s.FirstName = "Samuel"; 
    s.LastName = "Neff"; 
    s.Gpa = 1.2; 
    return s; 
  } 
} 
 
public class Student 
{ 
  private string _firstName; 
  private string _lastName; 
  private double _gpa; 
 
  public string FirstName 
  { 
    get 
    { 
      return _firstName; 
    } 
    set 
    { 
      _firstName = value; 
    } 
  } 
 
  public string LastName 
  { 
    get 
    { 
      return _lastName; 
    } 
    set 
    { 
      _lastName = value; 
    } 
  } 
 
  public double Gpa 
  { 
    get 
    { 
      return _gpa; 
    } 
    set 
    { 
      _gpa = value; 
    } 
  } 
}

Note the one issue we ran into is that .NET and AS3 have different naming conventions. In .NET we use FirstName for properties and in AS3 we’re supposed to use firstName. It’d be nice if the web service client translated for us, but it doesn’t. If you’d like to vote for that change, you can do so here: SDK-12917: SchemaTypeRegistry should allow language-appropriate member mapping.

Mp3 Download freeBest mp3 free downloadDownload music libraly freePharmacy Blog HosterBuy ViagraDrugstore OnlineOne Pharmacy ViagraTrust MEDICINESoftware newsAuto newsLaptop Computers
E-books blogThe Latest Tech and Gadget Newswho cooks foodFree Home Loan HelpWATCH 4 BEAUTYLATEST HARDWARE NEWSProtect Your MacNew Software Additionssoftware newsfree music downloadfemale healthmy medicabiotechnologysoft for designsoft and musicdating serviceimmovable propertyfree CAD programs

5 Responses to “Use Custom Value Objects with Web Services in Flex 3 Beta 2 !”

  1. Use Custom Value Objects with Web Services in Flex 3 Beta 2 ! Says:

    […] You can read the rest of this blog post by going to the original source, here […]

  2. Neil Finlayson Says:

    Suppose we are trying to decode XML into a strongly typed Value Object instead (ie not consuming a SOAP response, just trying to implement XML/Value Object mapping automagically). We have a schema and QName to allow us to register the class/element mapping with SchemaTypeRegistry. How do we then invoke an XMLDecoder which is aware of the mapping?

    SimpleXMLDecoder will decode into ActionScript objects for case where schema is *not* available. Can you show us pattern for when schema is available.

  3. Samuel Neff Says:

    Neil,

    Good question. Unfortunately the source for RPC components including WebService and XMLDecoder are still not included with the SDK so there’s no easy to see what calls WebService makes to decode the XML and also reference the XSD. Also the docs don’t seem to help.

    I created a bug in for Flex 3 SDK to document this relationship. You can vote for it.

    https://bugs.adobe.com/jira/browse/SDK-13101

    Best regards,

    Sam

  4. Lex Says:

    This help me with half of my problem. I get the the object from server to client. But I can’t get a object from the client to the server. So if you wanted to extend you example and add a new method in your web service public void CreateStudent( Student newStudent ). How do you get the typed object back up to the server??? I keep getting IO errors.

  5. Samuel Neff Says:

    Lex,

    Sending from client to server actually requires no special coding and works the same with typed and untyped objects. Using the above example I created a method EchoStudent() in C# which accepts a Student object and was able to call it from Flex sending either a Student object or a plain Object with the right methods–both worked fine.

    If you’re having trouble sending data from Flex to .NET then I would suggest trying the Flexcoders mailing list and be sure to include a detailed description of any error you’re getting, the WSDL for the service, and the SOAP being sent (both request and response). You can use Charles or Fiddler to get the SOAP content (or any other HTTP proxy).

    HTH,

    Sam

Leave a Reply