Mehfuz's WebLog

Live crazy, think different!

Sponsors

News

Passionate about cutting edge technologies and facinated by the modern web and phone revolution.Currently working at Telerik Corporation, the leading .net component vendor.
Follow me


Articles


Projects

REST to collection builder using LINQ to XML

Few weeks ago, I did a post about how can I handle REST response using LINQ To XML. You can take a look at it here.

http://weblogs.asp.net/mehfuzh/archive/2008/01/11/rest-with-linq-to-xml.aspx

Now , this is nice that we can parse REST response with LINQ To XML but at the same time it is kind of boring writing the mapping manually every time. In my Linq.Flckr project, I need to build objects from REST in various occasions, so its kind of monotonous as well as error prone writing the XML to Object Mapping manually, even its really easy to do using LINQ to XML. Therefore, I have come up with a tiny class that does the task for me and in return, all I have to do is to declare some attributes on top of my Class and Property.

Once, I have added all the property mappings, all is left is to create the RestCollectionBuilder object and call the ToCollecton to get the IEnumreable<T> result from REST response.

RestToCollectionBuilder<SomeObject> builder = 
new RestToCollectionBuilder<SomeObject>();
OR
// pass the root element from which attributes 
//will be passed to the SomeObject class.
RestToCollectionBuilder<SomeObject> builder =
 new RestToCollectionBuilder<SomeObject>(##ROOELEMENT##);
 // Where T: IDisposable
// Build the object.
IEnumerable<SomeObject> list = builder.ToCollection(##REQUESTURL##);
OR
// get the element and do some parse of your own, if needed.
XElement element = GetElement(..) 
IEnumerable<SomeObject> list = builder.ToCollection(element);

Talking about attributes and elements , let's consider the following REST response from Flickr .

<rsp stat="ok">
  <person id="12037949754@N01" nsid="12037949754@N01"
 isadmin="1" ispro="1" iconserver="122" iconfarm="1"
 gender="M" ignored="0" contact="0" 
friend="0" family="0" revcontact="0"
 revfriend="0" revfamily="0">
    <username>bees</username>
    <realname>Cal! Henderson@</realname>
    <mbox_sha1sum>2971b1c2fd1d4f0e8f99c167cd85d522a614b07b</mbox_sha1sum>
    <location>San Francisco, USA</location>
    <photosurl>http://www.flickr.com/photos/bees/</photosurl>
    <profileurl>http://www.flickr.com/people/bees/</profileurl>
    <mobileurl>http://m.flickr.com/photostream.gne?id=287</mobileurl>
  </person> 
</rsp>

Here , we need to tell the Collection builder which element is mapped to which property.Accordingly, I have created some custom attributes that will be used by the builder to create the map. Which are

  • XElementAttribute
  • XAttributeAttribute
  • XNameAttribtute

The first two attributes inherits the XNameAttribute. So, if our class is People and we need to map the person element and its descendants to it, our class will look like

// maps the element to which the class belongs to
[XElement("person")] 
public class Person
{
    // maps to <person nsId ="xx">
    [XAttribute("nsid")]
    public string Id { get; set; }
   // maps to <person ispro ="True">
    [XAttribute("ispro")]
    public bool IsPro { get; set; }
    // maps to <username>Henderson</username>
    [XElement("username")]
    public string Username { get; set; }
   
   ....
   ....
   ....
}

And that's all is needed to map the class to REST elements though under the hood, RestToCollectionBuilder class does use LINQ To XML to prepare the collection, but gives out a more strong solution to work with REST responses. I have used this technique in Linq.Flickr. You can check that out by digging in the code. Along with that, I have fused in a tiny Console App that processes the REST response, builds the object and dumps the result to the screen, you can download it here as well.

kick it on DotNetKicks.com
Posted: Mar 30 2008, 03:57 PM by mehfuzh | with 4 comment(s) |
Filed under: , ,

Comments

Kazi Manzur Rashid said:

Why do I need IDisposable, Why not the constrain is simple new?

# March 30, 2008 9:49 AM

mehfuzh said:

Infact , you don't need it. class someClass<T> where T: SomeType, is used to narrow down objects, so that only a particular type can used for the operation. The class is from Linq.Flickr , where i ensure that all query class is IDisposable, but here you don't need it, so i have modified the code reference without implenting IDisposable.

Thanks again, for the comment. 

# March 31, 2008 2:00 AM

NewQ said:

Really made my day with this code!!Do you have some utility which could construct the XML(with namespaces) using LINQ in the same way

# April 22, 2008 11:02 PM