What’s the recommended method of accessing Plone persistent utilities and why?
-
getToolByName(context, “portal_url”)
-
direct acquisition: context.portal_url
-
ITools interface
etc.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I think that the answer is in the code of
getToolByNameitself ( http://svn.zope.org/Products.CMFCore/trunk/Products/CMFCore/utils.py?view=markup ). That method does this:getUtilityand including it in a context (that seems a good thing to me)So the only method “to rule them all” seems to be:
getToolByNameBut, 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)