views:

40

answers:

2

I have a bunch of insert, update, and delete operations I need to execute via Ajax. Which of the following would be a better approach?

  1. Individual methods for each function (e.g. delete_foo, insert_foo, update_foo, delete_bar, insert_bar, update_bar, etc.)

  2. A "master" method and just pass a parameter to distinguish between operations.

A benefit of #2's approach would be that common things in the individual methods, such as validation or id decryption, etc. could be consolidated. However, it would also mean this master method would fairly large.

Having a master method call the individual method wouldn't be such a great idea, I think. The reason is that the individual methods, if stripped of the common tasks now handled by the master method, are one-liner codes (for the most part).

Maybe there are other things I didn't consider. Suggestions?

+2  A: 

Something you should consider is that something destructive - such as a delete - should never be entirely controlled by a GET request. You should always use POST parameters for something like that. Beyond that i would say its entirely down to personal preference and what logically seems correct for your system. For example if you were using something like Zend Framework you could have separate actions for each operation but use a common set of validatons/decryptions within your controller within your constructor/init methods

seengee
I'm using CodeIgniter framework. So, I will be using its validation scheme, etc. How does GET make a delete more dangerous than when done with POST?
StackOverflowNewbie
see these links: http://stackoverflow.com/questions/198462/get-versus-post-in-terms-of-security/1744404#1744404 and http://stackoverflow.com/questions/198462/get-versus-post-in-terms-of-security/198565#198565
seengee
+2  A: 

An alternative to both of these methods is to use the REST rules. This makes use of the other HTTP Methods other than GET and POST.

For example

GET /foo            --select foo
POST /foo           --this would insert
PUT /foo            --this would update
DELETE /foo         --this would delete

GET /bar            --select bar
POST /bar           --this would insert
PUT /bar            --this would update
DELETE /bar         --this would delete

More details can be found here http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services.

Codemwnci
The OP said he was using AJAX - i'm pretty sure most JavaScript AJAX libraries are still lacking proper support for PUT and DELETE
seengee
I was assuming that he was using JQuery to perform the ajax requests which does support PUT and DELETE
Codemwnci
yep you are right, its the browser support that is flaky. I knew there were issues with it somewhere!
seengee
I am using jQuery, but have been using POST/GET only. Is using PUT/DELETE a matter of semantics only? I've been able to do everything I need via POST in the past. Why use PUT/DELETE?
StackOverflowNewbie
POST works absolutely fine. PUT and DELETE were originally intended for this purpose though. The URL identifies the resource you want to modify, and the method specify what you do to it. REST is an attempt to use HTTP as it was intended to, and as a result make development make a little more sense. The wiki link I posted is a good read of the pros and cons of using this approach.
Codemwnci