Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

easy one regarding scope and cfinvoke

Community Beginner ,
Jul 01, 2014 Jul 01, 2014

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)>

300
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 01, 2014 Jul 01, 2014
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources