views:

53

answers:

1

Hi there,

I wonder if anyone can confirm the naming convention that i am using is correct, i have just started and really don't want to get into a bad habit

Here is what i have ... (see comments)

basically i have a mehtod called GetTasks but the uri is Tasks - i presume this is the way to go??

Also i have a method called GetUser where the Uri is (plural) Users/{id}

Any confirmation before i continue would be great.. thanks..

here are the methods i have currently..

    [WebGet(UriTemplate = "Tasks")]
    public List<SampleItem> GetTasks()  //RETURNS a COLLECTION
    {
        // TODO: Replace the current implementation to return a collection of SampleItem instances
        return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
    }


    [WebGet(UriTemplate = "Users/{id}")]
    public SampleItem GetUser(string id) // RETURNS A USER
    {
        // TODO: Return the instance of SampleItem with the given id
        //throw new NotImplementedException();
        return new SampleItem() {Id = 1, StringValue = "Hello"};
    }

    [WebInvoke(UriTemplate = "Users/{id}", Method = "PUT")]
    public SampleItem UpdateUser(string id, SampleItem instance) // UPDATES A USER
    {
        // TODO: Update the given instance of SampleItem in the collection
        throw new NotImplementedException();
    }

    [WebInvoke(UriTemplate = "Users/{id}", Method = "DELETE")]
    public void DeleteUser(string id) // DELETES A USER
    {
        // TODO: Remove the instance of SampleItem with the given id from the collection
        throw new NotImplementedException();
    }
A: 

Well, sometimes imitation is the best form of flattery. If you look at StackOverflow, they use users/{userid}/... for their URI convention. Generally, using plurals for the entities and then letting the next segment (if present) describe the action seems to be fairly standard. That's usually the direction I go in defining RESTful services:

GET      /Users         -- return all users
GET      /Users/{id}    -- return a single user
POST     /Users         -- insert a user
PUT      /Users/{id}    -- update a user
DELETE   /Users/{id}    -- delete a user

This lends itself to WCF Data Services pretty well, too. Although you define the entity names, the pattern is generally the same. The nice thing is that you can also define queries on the URL for your GETs, and it follows the pattern again:

GET     /Users?$top=10&filter=name eq 'Steve'  -- return top 10 users who's name is steve

So I think you're going in the right direction. Have you considered WCF Data Services? It builds those entity operations for you -- but it may not be the right solution depending on your requirements.

Good luck. Hope this helps.

David Hoerster