1 Reply Latest reply on Jul 1, 2014 10:18 AM by Carl Von Stetten

    easy one regarding scope and cfinvoke

    wannab0133

      Why does this work?

      <cfset projectList = Application.Projects.getProjects(CompanyID=request.User.CompanyID)>

       

      BUT, this throws an error?(Could not find the ColdFusion component or interface Application.Projects)

      <cfinvoke component="Application.Projects" method="getProjects" returnvariable="projectList">

      <cfinvokeargument name="companyID" value="#request.User.CompanyID#">

      </cfinvoke>

       

      I understand that cfinvoke is trying to look for an 'Application' directory, correct?  The Projects.cfc is located within a directory names 'sys'.  Would it be a better idea to use:

      <cfinvoke component="sys.Projects" method="getProjects" returnvariable="projectList">

      <cfinvokeargument name="companyID" value="#request.User.CompanyID#">

      </cfinvoke>

       

      Basically, is the above the same as:

      <cfset projectList = Application.Projects.getProjects(CompanyID=request.User.CompanyID)>

        • 1. Re: easy one regarding scope and cfinvoke
          Carl Von Stetten Adobe Community Professional & MVP

          The first statement works, because the Projects object is already created and stored in the Application scope (probably there is a CreateObject() call somewhere in the methods of your Application.cfc that created the object).  So "Application" in "Application.Projects.getProjects()" is not part of a dot-delimited path, like you use in <cfinvoke>.  Its a scope reference.

           

          In your <cfinvoke>, your dot-delimited path would be "sys.Projects", as you indicated in your third paragraph.

           

          The difference between the two is this: the <cfset projectList...> code snippet is calling a method of an already instantiated object that has been stored in the Application scope; the <cfinvoke> snippet is instantiating a new instance of the object, calling the method, and then throwing away the object, and will do this every time the snippet is called.  Therefore, the first approach is likely to provide better performance (the Project object only gets created once).  However, you must be sure that the Project.cfc is written in a thread-safe manner; otherwise, you run the risk of "race conditions" and unexpected return values.

           

          -Carl V.

          1 person found this helpful