Cairngorm and Flex Builder’s magical WSDL tool

So, I’m working with a client doing some development and mentoring. A piece of this is implementing the Cairngorm micro-architecture and mentoring duties surrounding this. The client’s developers made full use of the new WSDL import tool in Flex Builder 3 and are really sold on using it. Now, for those not paying attention here, the title did mention Flex Builder’s magical WSDL tool, Flex builder 3 has this really neato, down-and-dirty, quick-and-easy WSDL tool. Look under the Data menu item and choose Import Web Service(WSDL). You’ll get a little wizard GUI where you pick the project and source folder to put the code in, enter a WSDL location, choose what methods in the WSDL you want to include in the magical WSDL import tour, mash a button, and sit back and enjoy. After its done, take a look at the code generated…holy FSCK Batman!!!! Yeah, a whole butt load of code is generated. Totally bad ass, eh? Unfortunately its all generated in one folder, VOs, Events, service, etc. But hey, you can’t have it all in a badd ass world! So, how to use it?

You should see a file called something like Admin.as in there where mainclass is the name of the main class you supplied to the UI wizard a moment ago. Open this file up and you will see a example of how to use the code. As a added touch, the example actually uses one of the real WSDL methods! In AS its three lines of code looking like this
var myService:MyAdmin= new MyAdmin();
myService.addGetSessionEventListener(myResultHandlingFunction);
myService.GetSession(myUserName,myPassword);

where GetSession() is a method in my WSDL. That’s it! Uber kewl eh?

So, in just a few seconds your complete Service layer is written. To add more icing to this cake, all the service calls return TYPED OBJECTS FOR YOU! The type is based on your WSDL scheme. Thus you literally need only bolt on your UI and voila, impress your boss.

The downside: as this code is generated and we live in such a API imperfect world, you may have to regenerate it from time to time when your WSDL changes. So, you can’t really modify the code without worrying about maintaining it. Sure, in some cases you can play extend the class, but not always as some things are private Private! Like most magical gen tools, in my experience, they are always a canned solution. Meaning, they are designed to do one thing, not all the things your client may want. In this case though, things seem pretty cool as its the Service layer after all.

But wait! There’s more? Does it chop and shred lettuce too? ha, afraid not, but there is more. Most likely you will be using a framework in your app and most likely it will be Cairngorm, eh? I know, a bit jaded. So, how to tie in this code to Cairngorm use? Use the Delegate baby! In the generated code, find a file named BaseAdmin.as. This is the low-level layer in the code that handles the actual web service calls. Each method in here actually returns a AsynchToken, yo uknow that class your Delegate likes to work with. Thus we have code in our delegate like so
var service:BaseMyAdmin = new BaseMyAdmin();
var token:AsychToken = service.getSession(username, password);
prepareResponder( token, resultHandler, faultHandler);

you that’s it folks. I’m willing to bet the other frameworks out there PureMVC, ARP, Mate, etc… all work with tokens at this level, so should be just as easy.

To recap, we have a quick and easy way to work with webservices and your favorite framework with two major gains:

  1. Typed Objects from WSDL calls
  2. Time Savings

Others may see more benefits, but these are key to me, #1 especially as I don’t have to manually parse through XML creating typed objects.

back to coding!

peas

DK


8 Responses to “Cairngorm and Flex Builder’s magical WSDL tool”

  1. jack
    May 15th, 2008 | 6:35 am

    what fucking admin.as file man?? can’t fint it dude

  2. dano
    May 16th, 2008 | 6:15 am

    just replace “admin” with whatever your webservice name is.

    Thanks for the tips DK.

  3. June 6th, 2008 | 7:53 pm

    Great post. Ive been looking for ways to implement this nifty tool with Cairngorm, MVCS, or Universal Mind’s Cairngorm Extensions. Excellent. All of the delegates we have used have not used the AsynchToken (that I can tell). I’ll need to see how it can be used instead of the IResponders we typically use.

  4. DM
    October 3rd, 2008 | 10:39 pm

    Could you add the code for PrepareResponder?
    Any ideas on how to use this with a ServiceLocator?

  5. Jim
    March 16th, 2009 | 12:21 pm

    DK,
    Great post. Unfortunately I’m pretty new to Flex etc. I was tasked with figuring out how to make Flex talk to our PeopleSoft system. I was able to export a web service and see that PS created a wsdl that Flex could read and import. There are all kinds of Base***.as and servicename.as files as you described above.

    I’m trying to just put up a simple page with a prompt for ID and call the webservice through Flex mx: calls (as opposed to writing pure ActionScript) and display what comes back in a datagrid. I have been able to get Flex to call the web service (a couple of ways) but never with a parameter passed - PS always sends back the entire list of IDs as if you did a search with a blank search key. So I get a long list of user IDs in my data grid…

    The first way I tried was using the suggested sample code in the main .as file as you mentioned in your post. It is essentially:

    {compID.text}

    where I thought that the mx:request tag was how you passed the parameter. But DL_CS_COMPID never arrives at the PS end; you get this:

    Then I started reading through those .as files and looking and thinking and saw stuff like FindIDResultEvent (instead of just ResultEvent) defined, and FindID_request_var instead of just request_var. After considerable trial and error, I coded something that passes XML to PS and returns all records again, using an extra xmlns line on the mx:Application:

    xmlns:ws=”com.draper.*” (the place Flex put all those files)

    It looks something like this:

    {xxx}

    In this case it gets closer, as the XML at least mentions DL_CS_COMPID:

    I haven’t figured out how to display the data in the data grid. I see the grid expand, and there is a scroll bar, showing that lots of data came back, and I can see in PS the XML it sent with all those IDs…

    One really confusing thing to me: the Flex import built a bunch of .as files with DL_COMP_IDTypeDef and DL_COMP_IDShapeDef (DL_COMP_ID is the name of the search field that PS is expecting) as if the wsdl contained information about it. But the term DL_COMP_ID never once shows up in the wsdl, so I’m wondering how Flex knows about it - are there further calls to the wsdl source to get more info? How does it figure out that DL_COMP_ID should be a string of max length 30, as mentioned in one of those TypeDef files?

    I would really appreciate any guidance you can offer. I feel like I’m so close, but just can’t put the last pieces together…

    Thanks,

    Jim

  6. Jim
    March 16th, 2009 | 12:22 pm

    Oops. All my xml was stripped out and my paragraphs were reformatted. Sorry.

  7. frank
    March 20th, 2009 | 7:56 pm

    Hi,
    thank you for your Post. It works fine, but i’m unable to catch the results. How do you implement the “prepareResponder”?
    thanx in advanced
    frank

  8. Cedric
    May 22nd, 2009 | 9:05 am

    Jim..

    Did you ever get your peoplesoft stuff running? I’m running into problems myself. If you did come up with a solution, I would love to hear how and what you did to make it work.

    thanks
    Cedric

Leave a reply