4/06/2008

Super-Simple REST

It doesn't get much easier than this...

I'm using Mindtouch Dream as a REST host & SubSonic as a data access layer. I wrote a helper class to convert the
IDataReader results to an XDoc (Mindtouch's lightweight XMLDocument object similar to MS's new XDocument).
Keep in mind that this is an ActiveRecord pattern, so I am only returning the results of a single table.

 
[DreamFeature("GET:modules/{id}", "Get modules.")]
public Yield GetModuleById(DreamContext context, DreamMessage request, Result response)
{
XDoc doc;
int id = context.GetParam("id");

using (IDataReader reader = new Select().From("core_module").Where("id").IsEqualTo(id).ExecuteReader())
doc = XDocHelper.GetXDoc(reader, "Module", true, "BaseUri", "create_date");

response.Return(DreamMessage.Ok(doc));

yield break;
}

The helper class is smart enough to convert database columns ("modified_date") to camelcase names ("ModifiedDate").
And I added an option to allow you to specify which columns should be XML Attributes. The method signature is:
 
public static XDoc GetXDoc(IDataReader reader, string rootNodeTag, string itemNodeTag, bool useCamelCase, params string[] attributeColumns)

In this example, I told it to use camelcase, and both column names "BaseUri" and "create_date" should be attributes:

doc = XDocHelper.GetXDoc(reader, "Module", true, "BaseUri", "create_date");

This results in an XDoc created with the following Xml


1
2008-04-01T04:12:52Z
1
none
tempuri
true

0 comments: