Demystifying The Code

Exposing ATOM feeds from your services – part II – customizing and extending the ATOM feed

In yesterday’s post, I illustrated using the WCF REST Starter kit to create a RESTful service that exposes a sample ATOM feed.  We accomplished this by simply creating a new service using the ‘WCF ATOM Feed Service’ item template.  This template created an svc file with a REST-friendly factory.  It also created a cs file with one operation that returned an Atom10FeedFormatter.  The feed contained dummy syndication items.  In this post, I will show you how to update this code to expose custom data.  In this case, we will expose a feed of wines from a wine catalog.

 

Julia Child

Because this post doesn’t deal with data access, I felt OK with pulling a Julia Child and doing some cooking during the commercial.  If my metaphor is lost on you, what I mean is that I have already written the data access code and am not going to spend much time on it.  Essentially, I am using LINQ to SQL to access the database and I have a static class the encapsulates my logic.  Here are the signatures (they should be self-explanatory):

image

The only method we are going to use is the GetWines.  This returns a WineGroupingData type seen here:

image

This type contains a collection of WineDatas, seen below.  It also stores some data about what page of wines that were returned (useful in paging solutions – ex. page 1-10 of 55). 

image

The WineData type contains some primatives that are wine-related.

 

Updating the Sample

Now it is time to start updating the sample to return an ATOM feed that represents a page of wines.

Step 1 – Update the Method signature and the UriTemplate

Below you will see that I changed the name of the method to GetWineFeed and updated the signature to take in a startIndex and a pageSize int argument.  I further updated the UriTemplate to something a bit more friendly.  Note that I am mapping URI parameters down to the method arguments.  For instance the URI: www.robbagby.com/WineService.svc/wines?index=0&pageSize=5 will result in the startIndex and pageSize parameters being set to 0 and 5.

image

Step 2 – Get the data

I have added 2 lines of code here.  The first is declaring an XmlSyndicationContent variable.  We will use this variable for our serialized wines.  The second line simply fetches the data.

 image

Step 3 – Iterate over our wines, create xml content (our extensions) and create SyndicationItems

The first thing we do here is to iterate over the wines in our data.  For each wine, we want to create a SyndicationItem.  However, as I said earlier, ATOM supports protocol extensibility.  Essentially, we can add XML elements at the item level (or at the feed for that matter).  The only rule is that the namespace does not match the root namespace of the feed.

We are going to use a class called XmlSyndicationContent for our custom xml extensions.  We are using the overload where we pass in the MIME content type (application/xml) and a SyndicationElementExtension.  We are initializing this helper object with our wine object.

The next few lines of code use the UriTemplate object to calculate a Uri for each wine using a “wine/{wineID}” template.  This Uri will be used as the unique id of the SyndicationItem.

Once we have the xml and the Uri, we can create the SyndicationItem and add it to the collection.  Again, we are using an object initializer to create an instance of the SyndicationItem and set it’s properties.  Notice that instead of setting the Content to a TextSyndicationContent like the template defaults to, we are setting it to our xml.  This is our extension.  We set the Id of the item to the Uri we calculated earlier.  We set the title to the wine name.

image

Step 4 – Update the properties of the feed

The last thing to do is to update the id and the title of the feed itself.  I used the uri to the service as the id.

image

 

Test It

By issuing a GET to our service, you can see that we have our ATOM feed, complete with extensions.

image

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

Demystifying The Code