0

I'm developing a WCF REST web service using C# and .NET 4.0 framework.

I'm not sure about how to design the web service.

On my system there are activities and users. Users can do activities: ride a bicycle, play football, go out with friends, ...

This is userActivity class:

[DataContract]
public class UserActivity
{
    [DataMember]
    public int userId { get; set; }

    [DataMember]
    public int activityId { get; set; }
}

And this how I have designed the web service:

enter image description here

Is it correct to do it this way? I know, I can do wherever I want, but I'm not sure if my web service is well designed.

Maybe, I can add an activity to a user this way (changing POST method):

enter image description here

What do you think?

VansFannel
  • 45,055
  • 107
  • 359
  • 626

1 Answers1

1

I would not represent the UserActivity in the class model, but put a list of activities into the User class. Then you can put proper semantic in the URI design:

/users
/user/{userId}
/user/{userId}/activities
/user/{userId}/activity/{activityId}

The above URIs are self explanatory, you just need to put the verbs.

  • Thanks for your answer. Do you use this URI `/user/{userId}/activity/{activityId}` to add an **Activity** to a **User**? – VansFannel Sep 07 '13 at 14:41
  • Yes, using the PUT verb. Cheers. – user2737221 Sep 07 '13 at 17:29
  • Why do I have to use PUT? Can I use POST? – VansFannel Sep 07 '13 at 18:32
  • 1
    Indeed there is not a standard about REST, but the common accepted practice is to use PUT for create a resource in case you know the ID. Otherwise you use POST. – user2737221 Sep 07 '13 at 20:47
  • 1
    It depends the meaning you're giving to the user's activity. If you think the user's activity is not a resource, hence you're updatig the User resource, then you also need to taken into account what happens if you try to add the same activity again. POST is not idempotent, while PUT is. A couple of references: http://restcookbook.com/HTTP%20Methods/put-vs-post/ and http://stackoverflow.com/questions/630453/put-vs-post-in-rest – user2737221 Sep 07 '13 at 20:54