2011-08-08 11 views
0

二つのアプローチ:App Engine CRUD with webapp - 構造化に関する考え?

# routes: (passed to WSGIApplication) 
[..snip..] 
('/note/add', AddNoteHandler), 
('/note/delete/(.+)', DeleteNoteHandler), 
('/note/view/(.+)', ViewNoteHandler), 
('/note', ListNotesHandler), 
[..snip..] 

..対..

('/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 

(この場合、note)一部object上の各CRUD操作のための別のハンドラを有する繰り返される多くのコードになりますそれらのハンドラでは、そして巨大なルートリスト。一方、私は、これは、それを行うのすべての荒れ果てたやり方では、よりクリーンで、より良い構造を持っていると思う。

これについてのご意見はありますか?

+2

ちなみに、 "/note/(.+)/"はビュー、 "/note/(.+)/delete"は削除基準です。 –

答えて

2

コードを共有するすべてのハンドラに、ベースハンドラ(サブクラスwebapp.RequestHander)をサブクラス化させます。

このようにすれば、ルートとハンドラが正しく分離され、共通コードを除外することができます。

関連する問題