Two approaches:
# routes: (passed to WSGIApplication)
[..snip..]
('/note/add', AddNoteHandler),
('/note/delete/(.+)', DeleteNoteHandler),
('/note/view/(.+)', ViewNoteHandler),
('/note', ListNotesHandler),
[..snip..]
.. versus ..
('/note/(.*)', NoteHandler)
# which moves all the code from many RequestHandlers to one..
# ..but with a lot of branching inside, e.g.
class NoteHandler(webapp.RequestHandler):
def get(self, params):
params = params.split('/')
action = params[0]
开发者_开发知识库id = params[1]
# start switching by action
def post(self, params):
params.split('/'):
# POST case
Having separate handlers for each CRUD operation on some object
(in this case, note
) will result in a lot more code being repeated in those handlers, and a humongous route list. On the other hand, I feel this would have a much cleaner, nicer structure that the one-handler-for-all-of-crud way of doing it.
Any thoughts on this?
Have all of the handlers which share code subclass a base handler (which subclasses webapp.RequestHander
).
That way you both have the routes and handlers separated properly and can factor out common code.
精彩评论