[Zope-dev] make zope.component.registry.Components inherit from dict?

Chris McDonough chrism at plope.com
Sat Nov 28 11:48:42 EST 2009


Charlie Clark wrote:
> Am 24.11.2009, 04:24 Uhr, schrieb Chris McDonough <chrism at plope.com>:
> 
>> We've been handling some constructive criticisms from repoze.bfg  
>> developers
>> with respect to verbosity resulting from use of unnamed utility  
>> registrations
>> in a component architecture registry.
>> These criticisms, and our ameliorations are detailed here:
>> <http://docs.repoze.org/bfg/1.1/designdefense.html#bfg-uses-the-zope-component-architecture-zca>
> 
> An interesting document and discussion. Having struggled initially and in  
> some ways still struggling to grasp the ZCA I can understand the  
> temptation to try and hide it from developers "because they don't need it  
> to get their job done". But my own take on the ZCA is that the component  
> registry is nothing wildly complicated just significantly different from  
> what many people are used to. But once you know it's there and that it  
> just looks after components it's just like having a DBMS manage your data  
> for you: throw stuff into it and it will manage your stuff for you.
> 
> I recently gave a presentation on the ZCA to non-Zopers and the concepts  
> weren't too difficult for the audience (I find it helps to jazz up the  
> metaphors). Conceptually the biggest problems are probably the multi-key  
> (nature of component, interface, name) nature of the registry and the  
> function of an interface as the token or smartcard required to get the  
> desired component.
> 
> In the example
> 
>  from repoze.bfg.interfaces import ISettings
>  from zope.component import getUtility
> settings = getUtility(ISettings)
> 
> the biggest leap of faith is getUtility which makes a behind the scenes  
> call to which registry is present. 

Right.

> I can imagine a clearer implementation  
> that would do the same but be explicitly dependent upon a particular  
> registration.
> 
> registry.get((utility, ISettings, u''))

In a Zope app, you can use this pattern today to make that true:

   from zope.component import getSiteManager
   registry = getSiteManager()
   settings = registry.getUtility(ISettings)

In a BFG app the same thing would be best done as:

   from repoze.bfg.threadlocal import get_current_registry
   registry = get_current_registry()
   settings = registry.getUtility(ISettings)

(This is a little clearer maybe than using the "site manager" terminology.)

If a "request" object was available in that BFG app, it would be better done as:

   registry = request.registry
   settings = registry.getUtility(ISettings)

I agree that the extra verbosity of explicitly obtaining the registry and 
calling methods on it improves readability.  It's the API of the registry 
object I'd like to make better.  I'm not as concerned about the global ZCA API.

- C



More information about the Zope-Dev mailing list