I seem to regularly run into a model where I would like to change how
controller instantiation happens. A quite common patern I see is that
you want to do something with a subitem of a model, for example manage
the tasks for a particular project. With standard pylons you can easily
setup a route for a URL like /customer/<customer-id>/task/<task-id> and
map that to CustomerController.task(self, customer_id, task_id), where
you lookup the Customer model and then the right task for that customer.
This is then quickly duplicated when you add contracts, invoices, etc.
There is repetition there: every action method will start with code
to find the correct customer, like this:

    class CustomerController(BaseController):
        def DoSomething(self, cust_id, id):
            customer=meta.Session.query(Customer).get(cust_id)
            ...

        def DoMore(self, cust_id, id):
            customer=meta.Session.query(Customer).get(cust_id)
            ...

I wonder if it would be better if you could specify controller construction
parameters in the route. The route format could easily be extended so
support controller-parameters, for example by using @ instead of :. In
my example I could have a route like this: /:controller/@objid/:action/:id 
which would tell pylons to instantiate a controller with the objid parameter
(CustomerController(objid=123)) on which the action is called. Your code
would then look like this:

    class CustomerController(BaseController):
        def __init__(self, cust_id=None):
            if cust_id is not None:
               customer=meta.Session.query(Customer).get(cust_id)

        def DoSomething(self, id):
           ....

        def DoMore(self, id):
            ....

this reduces repitition and makes it possible to do (more) common setup
work in the controller constructor.

Wichert.

-- 
Wichert Akkerman <[EMAIL PROTECTED]>    It is simple to make things.
http://www.wiggy.net/                   It is hard to make things simple.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to