Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getToolByName() vs. others

Tags:

plone

What's the recommended method of accessing Plone persistent utilities and why?

  • getToolByName(context, "portal_url")

  • direct acquisition: context.portal_url

  • ITools interface

etc.

like image 331
Mikko Ohtamaa Avatar asked Mar 19 '12 06:03

Mikko Ohtamaa


2 Answers

I think that the answer is in the code of getToolByName itself ( http://svn.zope.org/Products.CMFCore/trunk/Products/CMFCore/utils.py?view=markup ). That method does this:

  • as a first attempt it tries to get the desired tool by looking up for the interface with getUtility and including it in a context (that seems a good thing to me)
  • as a fallback it tries to directly acquire the tool from the source context

So the only method "to rule them all" seems to be: getToolByName But, as @keul said, there's also the caching involved, and looking here ( http://collective-docs.readthedocs.org/en/latest/misc/context.html#itools-interface ) it is clear that using the ITools interface, when it doesn't fail due to tools that don't yet implement that interface, it's faster.

For the reasons above, in the end I'd suggest:

  1. ITools (faster)
  2. getToolByName (safer)

(as the direct acquisition is already tried by the getToolByName, if it fails, you won't have more fortune)

like image 192
Giacomo Spettoli Avatar answered Nov 05 '22 21:11

Giacomo Spettoli


The use of ITools utilities must be the best method, as they are cached. However this sometimes gave me problems (related to portal_membership tool, but I don't remember details) so in that rare cases I switch to getToolByName. The last one is the direct acquisition.

like image 42
keul Avatar answered Nov 05 '22 20:11

keul