The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








Latest Diggs - Programming

Computers Blogs - Blog Top Sites

Site Hits

Total: 2,795,178
since: 19 Jan 2005

Consuming an ASP.NET MVC POX Service from Cocoa

posted Mon 30 Jun 08

After giving up in my initial attempt to consume a WSDL service from the Mac, I decided to try out for myself what it would be like to consume a simple POX service (which is really nothing more than reading an XML document from disk or from an NSURL location). Turns out its really, really easy. 

The code is nearly line-for-line copied from Hillegass' book. What this means is that the sample uses the table view column identifier to store the XPath required to pull that column from an XML Node. This highlights exactly everything that I hate about books and code samples found online - sure, simple 'hello world' apps illustrate the syntax but they don't illustrate good patterns and practices. If you are learning from Hillegass' book, please take the time to convert his sample from using XPath column identifiers to using actual model objects that are loosely coupled from the underlying data source. Please, for the love of all that is decent about Cocoa.

Friends don't let friends code tightly coupled.

Here's the code that gets executed in response to pushing a fetch button in the demo app:

- (IBAction)fetchFoodStars:(id)sender
{
    [progress startAnimation:nil];

    NSString *urlString = @"http://testbox:9000/SampleData.mvc";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
                             cachePolicy:NSURLRequestReturnCacheDataElseLoad
                        timeoutInterval:30];

    NSData *urlData;
    NSURLResponse *response;
    NSError *error;

    urlData = [NSURLConnection sendSynchronousRequest:urlRequest
                                    returningResponse:&response
                                                error:&error];
    if (!urlData)
    {
        NSAlert *alert = [NSAlert alertWithError:error];
        [alert runModal];
        return;
    }   

[doc release];
   doc = [[NSXMLDocument alloc] initWithData:urlData options:0 error:&error];

    if (!doc) {
        NSAlert *alert = [NSAlert alertWithError:error];
        [alert runModal];
        return;
    }   
    NSLog(@"Doc = %@", doc);
    [itemNodes release];

    itemNodes = [[doc nodesForXPath:@"//FoodNetworkStar" error:&error] retain];
    [tableView reloadData];
    [progress stopAnimation:nil];
}

Basically what this code is doing is opening a URL that's pointing at the location of my ASP.NET MVC application which has a controller that knows it should be dealing in POX and not HTML (turns out that is scary easy, as will be detailed in the next post). 

Then this code converts the XML document retrieved into an array of XML nodes. The app controller acts as the data source for the table view, using the identifier value of each table view column as a key to retrieving the XPath that will retrieve the value for that column. For example, the 'name' column has an identifier of '@name', which tells the controller to get the value for the name column from the @name XPath (the name attribute on the current XML node).

Below you can see the results after pushing the fetch button. The table view took care of the binding and the call to reloadData on the tableview updated the values.

This is dead simple stuff. As I mentioned before, you do NOT want to be putting XPath in the column identifiers for your table views - that's tightly coupled HACKing. What you should be doing is converting the XML from your service into instances of your local data model (which is datasource-agnostic, isn't it???) and then your table view's data source will access the local data model objects instead of raw XML nodes. Once you get into trying to allow the user to interactively edit data that came from a web service, you'll thank me for recommending the local data model conversion.

In the next post, I'll show you the source code to the ASP.NET MVC application and the controller that I used as a POX service host. You'll be surprised that it was quicker and easier to implement the POX controller in ASP.NET MVC than it would've been in Ruby on Rails!

tags:                  

links: digg this    del.icio.us    technorati    reddit




1. Denis Ahrens left...
Tue 01 Jul 08 6:35 am

I tried to use WSDL Service some days ago and it went really fine until I noticed that it is not available on the iPhone (but you can use it on the Simulator). So what was your problem with WSDL Service?


2. MrScrith left...
Tue 01 Jul 08 9:27 am

POX.... wasn't familiar with that term so I googled it... first thing that comes up were images of bodies covered in boils and sores... thanks a lot.. :P


3. james Gregurich left...
Tue 01 Jul 08 12:33 pm

Having to use MS tools is like having a pox...so, the name is appropriate. :)


4. Erik D. left...
Sat 05 Jul 08 2:28 am

A while again, when people wanted to say "not SOAP" they used REST, which didn't make much sense, so people started saying "Plain Old XML" ie. POX. http://en.wikipedia.org/wiki/Plain_Old_XML


Tag Related Posts

Microsoft's Lofty Direction

Sun 05 Oct 08 2:30 P GMT-05

Apple drops the iPhone NDA for Released Software

Wed 01 Oct 08 3:54 P GMT-05
tags:          

Cappuccino, Objective-J, and You

Wed 10 Sep 08 6:14 P GMT-05

Enterprise Web Services Manifesto - Wire Formats

Tue 02 Sep 08 1:43 P GMT-05
tags:                

So I'm in the LA Times ;)

Wed 27 Aug 08 2:51 P GMT-05
tags:                  

MobileMe vs. Live Mesh Throwdown - Round 1

Wed 16 Jul 08 10:33 A GMT-05

Building Model Classes in C# and Cocoa

Sun 15 Jun 08 3:13 P GMT-05
tags:            

NYC SharePoint Developer Needed

Mon 12 May 08 12:09 P GMT-05

One Framework to Rule them All

Mon 25 Feb 08 6:49 P GMT-05

Scott Guthrie Updates the ASP.NET MVC Roadmap

Wed 13 Feb 08 3:49 P GMT-05
tags:    

Volta is to Ajax what Tums is to my Stomach

Wed 30 Jan 08 4:11 P GMT-05

LINQ to REST - A much better name for Astoria

Tue 11 Dec 07 1:23 P GMT-05
tags:        

ASP.NET 3.5 Extensions Preview Released

Mon 10 Dec 07 2:10 P GMT-05

Leopard Code Sample : Sprinkling in some Bonjour

Tue 27 Nov 07 2:32 P GMT-05
tags:        

Leopard Sample: A Bound NSCollectionView

Mon 29 Oct 07 1:41 A GMT-05

Leopard is out - let the code samples begin!

Fri 26 Oct 07 10:09 A GMT-05
tags:          

Microsoft unveils an MVC framework for ASP.NET

Mon 08 Oct 07 12:58 P GMT-05
tags:      

Microsoft Codename Acropolis - Unwrapped

Wed 20 Jun 07 3:22 P GMT-05
tags:              

The dreaded language bleed-over has begun

Tue 19 Jun 07 6:23 P GMT-05
tags:        

Exploring the Delegate Design Pattern

Mon 14 May 07 6:30 P GMT-05

Core Data - Almost too Easy?

Wed 18 Apr 07 2:23 P GMT-05

Exploring the MVC Pattern in WPF

Tue 10 Apr 07 12:51 P GMT-05
tags:                      

My Little Pony .NET Unleashed 2007

Fri 30 Mar 07 1:59 P GMT-05

An experience with the Leopard beta

Mon 26 Mar 07 7:45 P GMT-05
tags: