[z3-five] context-based utility lookup
Philipp von Weitershausen
philipp at weitershausen.de
Tue Feb 13 00:59:03 CET 2007
On 12 Feb 2007, at 18:21 , Chris Withers wrote:
>> IMO not having to specify a context is a good thing. Sometimes
>> code doesnt' have access to a context, but it still wants to
>> lookup a utility. Should it not be possible to let that utility
>> come from a local component registry? I as the one who makes up
>> application policy should be able to say where that utility comes
>> from.
>
> Indeed, but can you override the context that gets used to figure
> out what utility gets picked up?
Yes, you can always pass in a context explicitly in which case the
closest site to that context will be used for the lookup.
>>> If that's the case, then how do I make sure the right utilities
>>> are used when I'm traversing around several objects in different
>>> locations in the same piece of code?
>> There's only one kind of traversal -- URL traversal -- and it only
>> happens once per request.
>
> Ah, okay. I suspect I'm going to learn lots more about this when I
> come to refactoring the publications stuff...
Actually, the publication is quite agnostic of sites, fortunately.
All the publication does is send out an IBeforeTraverseEvent for each
object it is about to traverse. zope.app.component has a subscriber
for that event on ISites that "remembers" the site in a thread local.
In Zope 2, it works similarly, except that there's no publication
that sends this event. Currently, we install a BeforeTraversalHook
(something that the ZPublisher allows us to do) on an object-per-
object basis; this hook sends the event (and then the same subscriber
as in Zope 3 picks it up). This I'd actually like to change for Zope
2.11 so that we get before traverse events for all objects and don't
have to install hooks on persistent objects anymore.
More information about the z3-five
mailing list