On Sat, May 14, 2011 at 9:53 AM, Dmitriy <[email protected]> wrote:

> The class will expose their public methods for calling
> like functions. I want to make a controllers hierarchy. This will give
> a possibility to inherit methods from ancestors following the DRY
> principle :)
>

As I know (and as I believe) you are not encouraged to create nested
functions in controllers actions, and I do not think it is good for MVC
patters,
I think controllers actions has to be responsible only for the "control
flow" of the requested page. The "control flow" include object declarations,
data access and function/method calling, the result of this "control flow"
is what you "return" to the view.

For sure, you can declare classes inside controllers, but just like
parametrized and __protected functions this classes and methods will not be
visible or routed. (so this kind of function will not be called "action"
because it is not fired by the request)

The better way is to declare your class based logic under /models or even
better under /modules folder (which has new improvement comming in trunk),
so in controller actions you will just instantiate and call classes methods.


Example (dirty but tested):

*Defining a class in model*
---- /models/mylogic.py -----
class MyModelClass(object):
    def sum(self,x,y):
        self.total = x + y
--------------------------------------

*Defining a class in modules*
--- /modules/mymodule.py ---
from gluon import *
class MyModuleClass(object):
    def foo(self):
        return DIV(P("bar"))
----------------------------------------

*Defining a class in controller*
--- /applications/myapp/controllers/default.py ----
from mymodule import MyModuleClass
class ControllerClass(object):
    def controller_function(self):
        c = MyModuleClass()
        m = MyModelClass()
        return dict(moduleclass=c.foo(), controllerclass=m.sum())

#this is an action, just calls method and classes.
def mypage():
    o = ControllerClass()
    return o.controller_function()

-------------------------------------------------------------------

Reply via email to