Search This Blog

Monday, January 25, 2010

RESTful HTTP and the Representation

I was looking at a BLOG posting by Jacob Kaplan-Moss and found his Conflating models and resources an interesting point. When working with a technology like Hibernate, if we were to have an Object like SuperHero, when retreiving the same, where do we stop the retreival and let lazy loading kick in? For example,




class SuperHero {
private List<Power> powers;
private Set<Superhero> superBuddies;
private Set<Colleague> colleagues;
private Set<Trophy> trophies;
....
}

Do we load every thing when load "Superman"? That would be quite a lot of data. What Hibernate does is use proxies that will lazily load each attribute only when asked for. One could also perform queries such that only parts of the information are returned.


If one were working in a RESTful system, when one accessed the resource, /superheroes/superman what should one get back? Would we expect the entire Superman representation? In a deeply nested model, the cost of returning such data can be considerable. I think it is important to consider the idea of lazy loading when working in REST as well and especially the word "State Transfer". I imagine one would typically want to navigate a RESTful system through different states or URLS if in HTTP. For example,




/superheroes/superman
/superheroes/superman/powers
/superheroes/superman/powers/heatvision
/superheroes/superman/superBuddies
/superheroes/superman/superBudies/Green%20Lantern

etc etc


Every Representation along the way providing information that is sufficient enough to represent the resource being queried with links to additional immediate resources. This improves the "linkedness" of your application and allows one to traverse your application by entering and leaving different states. Should you need to represent superman in his entierity, then a resource to the equivalent of /superheroes/superman/full can serve to load the shebang of information :-)

No comments: