Articles

Thoughts on REST: Who gives a shit?

In .Net on August 9, 2010 by Matt Grande Tagged: , ,

I feel like I’ve come to a new understanding of REST.

I’ve long been an evangelical supporter of REST. I’ve felt that Controllers should have, at most, the Seven Actions (List, Show, Edit, Update, New, Create, Delete).

But no longer…

I’m currently working on a project that had a normal, everyday requirement. Purchase Orders can be edited, and they can also be closed. From the Pure-REST point-of-view, I have two options: A convoluted Update action that does two things or a creating a specific PurchaseOrderClose controller that does one highly-specific thing.

Instead, I opted to do something terrible and dangerous…

I added a Close method to my PurchaseOrder controller. I added a custom route (/PurchaseOrders/{id}/Close). The world didn’t explode, and the API is still easy to understand.

It’s a simple lesson, but one that’s often hard-learned: To every rule, there is always an exception. Always err on the side of cleaner code.

/// <summary>
/// This is it.  This is the one that will infect your soul, curve your spine,
/// and keep the country from winning the war. 
/// </summary>
public ActionResult Close(int id)
{
	var po = _poRepository.Close(id);
	return View(po);
}
Advertisements

One Response to “Thoughts on REST: Who gives a shit?”

  1. REST doesn’t define the actions, it defines the activity that should occur when a given HTTP Verb is used. So POST should always create, GET should always retrieve, DELETE should destroy, and PUT should update. I’m a huge fan of not sticking to 7 controller actions like they’re commandments, and making your code work in a readable manner. Adding a “close” method was the right choice from both the REST perspective, and an API design perspective.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: