Codecamp BA - RESTful Services

17.09.2010 13:48Comments
Since I already had the presentation from the CodeCamp Buenos Aires, I thought I’d translate it to English and explain what the presentation was about. REST has earned a place amongst applications that consume ‘resource-centric’ services. Microsoft has addressed this by giving WCF REST capabilities, and then took their commitment even further by lunching WCF DataServices and the OData Protocol. In this presentation we cover how to build RESTful services with WCF REST, WCF DataServices and even with ASP .NET MVC. RESTful Services Codecamp 2010 English

REST

REST which stands for “Representational State Transfer” is an architectural style for building web services over HTTP. By saying “architectural style” we mean that there are no specifications or standards that we need to follow in order to make a service RESTful; instead we have principles to adhere. The fact that these principles are not mandatory gives us a sense of RESTfulness, the more and the better we adhere to these principles, the more RESTful our service will be.

The five REST principles are:

  1. Identify resources with URIs.
  2. Use the standard HTTP verbs (GET, POST, PUT, DELETE) to operate over the resources.
  3. Link resources using their URIs.
  4. Expose the resource representation in multiple formats (xml, json, etc)
  5. Stateless Communication.
Here there are some examples of Principle number 1: http://myservice/rooms this could be the link to the list of all the rooms in a building. http://myservice/roomS/3 this could be the link to the room number 3.. http://myservice/Colors/red this could be the link to the color red. http://myservice/Transaction/1145001 and this could be our way of representing transactions in REST, by exposing the transaction as a resource and providing a proper representation. The four HTTP standard verbs could be used in the following way:
  • GET: Returns a representation of the resource
  • PUT: Updates the resource
  • POST: Creates a resource
  • DELETE: Deletes a resource
Whe can link resources to form higher-order resources: <order self=’http://example.com/orders/1234’> <amount>23</amount> <product ref=’http://example.com/products/4554’ /> <customer ref=’http://example.com/customers/1234’ /> </order> Notice how the id is represented by a Uri to itself. Ideally we should offer our consumers different formats of the representation of our resources. Upon their request of a particular format, the service should return the resource accordingly. GET /accounts('UBS') Host: restsamples.com Accept: application/atom+xml … GET /accounts('UBS') Host: restsamples.com Accept: application/json … And as for stateless communications, we should not keep a state of the communication, and this means no server-side sessions, and no client-side cookies. The service should act upon request. GET /accounts('ubs') Host: restsamples.com Accept: application/account+xml <account><contact ref="/contacts('ubs-12345')">...</contact></account> … GET /contacts('ubs-12345') Host: restsamples.com Accept: application/contact+xml <contact id="ubs-12345">...</contact>

Microsoft & REST

Microsoft currently offers three different flavors of RESTful services. Two of them are under the WCF umbrella: WCF WebHttp and WCF DataServices; and the other is ASP .NET MVC (yes, apart from websites being REST applications, MVC supports REST principles for services). In the next posts we'll take a look at each one:
  • WCF WebHttp. Full control over the Uri, Formats and protocols.
  • WCF DataServices. We design based on a Data Model, and we expose both IQueryables<T> members and IUpdatable behavior.
  • ASP .NET MVC. Apart from websites being REST, we explore how we can offer other formats like json.

comments powered by Disqus