Library tutorials & articles

REST and .NET 3.5 Part 1 - why REST based services?

What is REST?

REST is a model for creating services that embraces the model that the web uses. It is a model that is based on resources that are identified by a unique URI. Then, what you are intending to do with the resource is specified by a transport protocol verb. The model his formally not bound to HTTP but for all intents and purposes it currently is. The verbs generally used are: GET, PUT, DELETE and POST.

  • GET – Retrieve the resource
  • PUT – Update the resource
  • DELETE – Delete the resource
  •  POST – Create the resource

There is some debate about whether you should also use PUT for creation and POST also gives a general catch-all for operations that don't really conform to CRUD (Create, Read, Update, Delete) operations. Let's look at an example: we want to retrieve the list of products from the acme corp. Order system. SOAP sends a message, using HTTP POST, to the order system endpoint (say, http://www.acme.com/orderservice) with an action header of GetProducts. With REST we issue an HTTP GET to the URI http://www.acme.com/orderservice/products. It is the verb that determines what we are trying to do and the URI that determines what we are trying to do it to. But more than this, REST based systems also return the URIs for the next valid set of operations depending on the context of where we are in the application protocol. So let's look at why this model doesn't suffer from the issues with SOAP detailed above.

HTTP scales

The fact that each resource has a different URL does not imply that they should all be on the same host. There is no need for a single "endpoint". This allows us, for example, to horizontally partition our data on to separate server farms where customers A-M hit one set of machines and customers N-Z hit another. In addition all read operations (namely those that do not alter state on the server) use HTTP GET. GET operations can be cached using the HTTP 1.1 caching directives which allows the service to offload traffic on to the general web infrastructure.

The application protocol is built into the message exchange

A fundamental part of REST is that the next set of valid operations is encoded in the response from the last. This means that as long as you only use actions from the last operation you will always perform a valid operation according to the application protocol.

The URI gives you context

The URI does not have to be a static thing but is inherently a data driven entity. Let's use an expense claim service: we can create a new expense claim passing a number of line items using

POST http://acme.com/expenseclaim

This returns the next valid actions one of which could be submitting the expense claim to accounts. This action could be

POST http://acme.com/expenseclaim/submission/456

The 456 in the URI is the identifier for the created expense claim. The crucial thing here is that the user can save the URI and then go home for the night. In the morning they simply submit to the URI and the context of where they were in the expense claim submission is maintained.

The payload is whatever makes sense

REST does services very often use XML as a payload but it does not bind you to it if some other format makes more sense. For example, if an AJAX application wants to use your service it makes more sense to return the data as JSON than XML; if the resource is an image then use an image.

 

Comments

  1. 14 Nov 2008 at 18:36
    Is anyone interested in working on Windows Live Social Networking site? This is all done in C# and ASP.Net with a little Ajax. -Ryan Phillips engineerfinder@live.com
  2. 14 Nov 2008 at 17:42
    Erm, except Amazon exposes all of their APIs via SOAP. It's not really and apples-to-apples comparison either, because both Google and Yahoo cite ease of use/low barrier to entry as being a talking point on why they're not or are no longer supporting SOAP. The fact is, all the APIs that I know of that they're currently exposing are really targeting presentation layer logic and mashups. SOAP really wasn't designed to be as lightweight or included in client-side logic; by comparison, Amazon's services really target enterprise developers, and as such many more people wind up using SOAP to access their services than used it with Google's API when they were still offering the service. Google and Yahoo offer maps, search, and the like, while Amazon offers asynchronous queues (who uses queues for a lightweight mashup?), fault-tolerant storage, and scalable servers. I agree with most of what you're saying, just wanted to point that out; most sites aren't switching to REST because its merits, but because its lower learning curve draws in a larger potential developer base.
  3. 01 Jan 1999 at 00:00

    This thread is for discussions of REST and .NET 3.5 Part 1 - why REST based services?.

Leave a comment

Sign in or Join us (it's free).

Richard Blewett Richard is the CTO of DevelopMentor UK. He began life as a mainframe programmer writing ALGOL and COBOL, but jumped to OS/2 after a year. In 1995, he started developing under Windows and got his fi...
AddThis

Related podcasts

Events coming up

  • Nov 18

    15 Minutes of Fame

    Dresher, United States

    This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.

We'd love to hear what you think! Submit ideas or give us feedback