2

I have the following resources in my Asp.net web API:

route: consisting of the following relevant properties

  • arrival_location_id
  • leaving_location_id

and location:

  • id
  • name
  • coordinates

The thing is, When I create a new route, I create many routes and I want to allow users to create a new locations if they don't already exist.

I have two different approaches that I can think of

Create an action that manages both

I would recieve something like the following json

{
       "routes":[
           { 
               "arrivalLocationId": 1,
               "leavingLocationId": -1
           },
           { 
               "arrivalLocationId": -1,
               "leavingLocationId": 2
           },
           { 
               "arrivalLocationId": -2,
               "leavingLocationId": 2
           }
           ],
        "locations":[
                { /*blablabla*/}
                { /*blablabla*/}
            ]
    }

The action will insert the locations, and than update the negative ids to the right id of each location.

Create two different actions

The other option is the more restful one, It requires two different calls to the server - first insert the new locations, and than insert the routes.

Pros and Cons

The first requires less calls to the Server, and therefore reduces data usage.

The last is more restful, and much more simplistic in terms of development - no need for different checks in the server side, probably easier also for the client side.

I assume that in my case it doesn't matter that much, but these kind of things create sort of a guideline for future developers, and it might make a difference in the future -

so the real question is -

should lowering data usage be a big factor in my API design or only when the difference is noticable?

  • 2
    Short answer: Favour simplicity until you need performance. Other than that, guidelines are guidelines. – Ant P Aug 16 '16 at 10:08
  • 1
    also consider whether you want the new locations and the route to be added atomically in a single transaction – Ewan Aug 16 '16 at 15:00
  • There isn't anything non-RESTful about the first option. Go with that. REST is concerned with transfer of state of resources between client and server, not the form the resources take. If you want a resource that has both routes and locations as part of its data format, go for it. – Cormac Mulhall Aug 19 '16 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.