What's the recommended method of accessing Plone persistent utilities and why?
getToolByName(context, "portal_url")
direct acquisition: context.portal_url
ITools interface
etc.
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:
getUtility
and including it in a context (that seems a good thing to me)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:
(as the direct acquisition is already tried by the getToolByName, if it fails, you won't have more fortune)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With