What is the difference between using cfinvoke and createObject to run a component function?

In my company balance sheet, I often saw component files used to initialize the object of this component and call methods from the object. However, it seems a little easier to use the cfinvoke method, especially if you use only one method from the component file. What are the differences between the two methods for calling a component function, and what are the pros and cons of each? When should I use which?

+6
coldfusion components design-decisions createobject cfinvoke
source share
4 answers

cfinvoke can only be used in tags.

createObject can be used in both tags and cfscript and tends to be slightly thinner / easier to read IMO.

Until recently, I avoided using cfinvoke because I found it to be cumbersome, but you can dynamically iterate over methods inside CFC about it. In createobject you cannot.

So, if, for example, I have a CFC that has methods - method1, method2, method3, method4. I can go through them like this: -

<cfloop from="1" to="4" index="element"> <cfif structKeyExists(this,'getMethod#element#')> <cfinvoke component="#this#" method="getLine#local.element#" returnVariable="methodValue"></cfinvoke> <cfset arrayAppend(myArray,methodValue) /> </cfif> 

-

Another thing to note is that some hosts share the lock on createobject. Mostly because of the access it provides for underlining Java.

+3
source share

Another advantage of using createObject() is that you can bind the init() method, for example.

 <cfset myObject = createObject("com.path.MyObject").init() /> 

And if your init() returns this , you can go ahead and bind the method if you don't need to use the object again:

 <cfset functionResults = createObject("com.path.MyObject").init().myFunction() /> 

It is worth noting that in CF 9 you can use the new syntax to create objects. For example, to create the same object as above and call it init() , I can write:

 <cfset myObject = new com.path.MyObject() /> 

This is neat and I like the option to do it. CF, in my opinion, is moving in the right direction with such features.

+8
source share

Instead of paraphrasing this discussion, I just point you to Google:

http://www.google.com/search?q=cfinvoke+vs+createobject

There are some subtle differences (IE: <cfinvoke> is able to handle dynamic method names), but essentially it just comes down to personal preference. Well, this is the fact that you cannot use <cfinvoke> through <cfscript> .

+4
source share

You almost answered it yourself: on the surface, you could say that if you call only one method on a page, then do it in one fell swoop in CFINVOKE (which creates an instance of CFC and calls the same method) make sense. And of course, if you call more than one CFC method on a page, then separating the steps makes sense (create an instance of CFC using the createobject function or cfobject tag, and then call the methods found in this object, a pointer to CFC), so you don’t pay this cost per copy more than once.

But keep in mind that if a page is called frequently, it may also make sense to save this result of creating a CFC instance so that it can be reused in a subsequent request to the page. You would do this by storing it (the result of cfobject / createobject) not in a local variable, but instead in a common area: whether it is a server, application or session, based on the who will benefit from such reuse. Of course, then you need to programmatically process / decide how long to keep this "cached" instance of CFC.

How important, when you save a CFC instance in this way, you become more susceptible to the var scope bug error, which basically is that you need to be more careful that the VAR creates any local variables created in CFC. Instead of trying to dwell on this in more detail, I will point to the meta-resource that I created on this:

http://www.carehart.org/blog/client/index.cfm/2010/3/4/resources_on_the_var_scope_problem

Hope this helps.

+3
source share

All Articles