In part 5 we looked at RESTful routing. In this part we continue to explore RESTful routing, by examining nested resources.
I originally planned to cover nested routes and to cover two questions posted to me in previous comments and emails. One question was with regards to ‘one to many relationships’ e.g. an album has many songs. The second question was from Tom in part 3; this was about adding save as functionality.
In trying to answer these questions I created a new application – a text editor with a version history. This has turned out to be a fairly interesting piece of work. I then changed my mind and decided to include it in a follow up post. I am hoping to have that out later this week (before Friday the 11th of July). As the ‘follow up’ post will have a practical example of nested resources, this post will not have the usual practical section with the experiments.
Part 6
Introduction to Nested Resources
The Nested URI
Returning to the familiar Music Store application, imagine we added songs to the application. Like Albums, Songs are resources and we could expect to access a song with this URI:
/songs/124
We could then perform the required CRUD operations on that URI based on the REST API as discussed in part 5.
An album has many songs. We can express this within the URI e.g.
/albums/10/songs/124
Once again we could make use of the HTTP verbs to perform CRUD in a RESTful way.
In this particular implementation, this URI does not mean that this is the 124th song of album 10 - it is possible for it to be the fist song of album 10. The URI is pointing to the 124th song in the system.
If we wanted the URI to state the song\track number of the specified album then we could alter the above implementation. Then we could expect to see URIs of:
/albums/10/songs/5
and
/albums/11/songs/5
This would not result in a URI conflict.
Continue reading →