Reputation: 18521
I have the following object
public class MyEntity{
public int id;
public string name;
public List<Trains> trains;
}
Now, a user changed the values of the trains list inside MyEntity.
He deleted two trains instances, updated names of two instances and inserted two new ones.
What is the best way to design the crud command restful style?
I can think of the following:
Option 1.
1 call, HTTP update of MyEntity, sending the full new trains list.
problem - the list can consist of thousands of unchanged elements - redundant.
Option 2
1 call, HTTP update of MyEntity, sending Only the changed elements in the trains list.
Sending also the type of the change i.e. the command will be UPDATE and the json will contain
1, "trainA", 'u' // update
2, "trainB", 'u'
3, "trainC", 'i' // insert
4, "trainD", 'i'
....
problem - the list I need to send the status
Option 3.
send separate restful with right commands for the inner object - as follows
delete .../MyEntity/3/train/1
delete .../MyEntity/3/train/2
update .../MyEntity/3/train/3 // +json
update .../MyEntity/3/train/4
insert .../MyEntity/3/train/5 // +json
insert .../MyEntity/3/train/6
Problems:
1. not transactional - multiple commands.
2. binding is more complicated - should be declared in the right order in order not to delete outer element.
What is the correct way to do it?
Thanks.
Upvotes: 0
Views: 147
Reputation: 2987
If I were you, I will go with option # 3. It will be multiple commands, yes, but atleast it's a lot more atomic and hence easier to understand for the clients/developers as well. Especially when each of your url will have a separate verb attached to it, for example:
GET /entity/{entityId}/train/{trainId} - Gets the train with trainId PUT /entity/{entityId}/train/{trainId} - updates the train with trainId POST /entity/{entityId}/train - creates a train and returns the created train with trainId DELETE /entity/{entityId}/train/{trainId} deletes the train with the trainId.
I am not sure what you mean by "declared in the right order". The CRUD on the trains should happen based off of the ids and not based on what order they are in inside some other entity. Hope that helps.
Upvotes: 1