Page 1 of 1

context object

Posted: Mon May 19, 2008 4:24 pm
by koen.h
Browsing through the agavi and symfony code I saw they used a context object while other popular frameworks do not. I'm a bit annoyed with passing objects like request and response all over the place so I thought the context object could be nice in dealing with that. The other option to get rid of these method arguments would be to use a global registry with getters and setters aimed at these frequently needed objects instead. The platform independence could then be reached in a similar matter.
Enyone with opinions about this choice?

Re: context object

Posted: Mon May 19, 2008 5:31 pm
by Christopher
I would choose a Registry over a Context object. A Registry will encourage modularity better.

Re: context object

Posted: Tue May 20, 2008 3:56 am
by koen.h
You're probably right. The coupling relationships with a registry are also very clear.

Re: context object

Posted: Wed May 21, 2008 3:16 am
by Maugrim_The_Reaper
The problem though, for the ZF at least, is that they are cornered. To maintain component independence they can only rely on components that are obviously related. Zend_Registry is not obviously related to Zend_Controller, hence they use setters over a Registry interface. Of course you can argue, as many of us do, that they spoil even that by adding such tight integration (by default) between the Controller and View (i.e. the ViewRenderer and it's Controller related render() method and configuration options).

Re: context object

Posted: Wed May 21, 2008 5:32 am
by koen.h
What do you mean by "they use setters over a Registry interface". AFAIK they use setters and getters in the front controller. Hence to get things like the dispatcher they use methods which call the singleton frontcontroller instance and get the dispatcher from there.
eg somewhere in zend_controller_action: $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();

If they use a registry for these things I haven't encountered it yet. Anyway, I don't like the above construction.

Re: context object

Posted: Wed May 21, 2008 7:07 am
by Maugrim_The_Reaper
Substitute "instead of" for "over a". Same meaning - sorry I get a bit obscure when typing at times ;).

Re: context object

Posted: Wed May 21, 2008 7:22 am
by koen.h
I see what you mean now with "over a". Being non-english it's sometimes easy to misread/misunderstand. The problem is not with your use of words.

Re: context object

Posted: Thu May 22, 2008 9:40 pm
by Ambush Commander
It sounds like ZF needs a healthy dose of the separated interface pattern. Instead of relying on the Registry component, they simply rely on a Registry interface.

I've always been a fan of context objects (effectively registry objects passed by parameter) because they are truly local. Registries are glorified globals and should be treated with care.

Re: context object

Posted: Thu May 22, 2008 11:09 pm
by Christopher
Context objects are still glorified globals. The only difference is the contract is with a method name rather than a registered name. That means a Context object is simply an inflexible, inextensible registry. Which you may want, or you may not want.

Re: context object

Posted: Fri May 23, 2008 2:44 am
by Maugrim_The_Reaper
Bear in mind, the Registry in ZF is global for one specific Registry instance. Many components utilise the Registry as the default Context class and chug along with numerous non-global instances.