Outside of stylistic differences there are a couple considerations:
When you use CFINVOKE in the way your test code uses it (in the calling
page, not inside the CFC), you are creating a fresh instance of your
component every time you call CFINVOKE. As you suspect, if you are going to
call multiple methods on the User component you will indeed be better off
using CFOBJECT and then calling the methods on the same instance (unless for
logic reasons you actually want new instances). Keep in mind, though, that
you can still use CFINVOKE with CFOBJECT:
<cfobject component="Foo" name="foo"...>
<cfinvoke component="#foo#"....>
Notice you can pass the actual instance instead of the name of the
component -- this allows you to use CFINVOKE on an existing instance.
As far as performance, the issue is not which of the tags you use, but
whether you need to make new instances (instantiation has some inherent
overhead -- and it's more for components that extend other components and/or
have lots of methods, apparently). The general rule of thumb is to create
new instances only when necessary. If possible, look into caching your
component instances in Application, Server, or Session scopes when
appropriate (which can be done the same way as with any variables).
Personally, I never use CFOBJECT or CFINVOKE. I prefer to use
createObject() and then call the methods using the "object syntax":
<cfscript>
foo = createObject("component","User");
foo.getUser(id);
</cfscript>
I find that easier to read and much easier to type, but that's a style
issue, not a performance issue.
HTH.
- Nathan
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Behalf Of Jay Gibb
> Sent: Friday, September 19, 2003 10:26 PM
> To: [EMAIL PROTECTED]
> Subject: [CFCDev] cfinvoke vs cfobject
>
>
> Hey everybody,
>
> I'm new to the list, so please excuse my question if it has already been
> answered (or feel free to point me to an online archive if one exists).
>
> I'm currently defining some coding conventions for a consulting firm, and
> I'd like to get everyone's opinion on the usage of cfinvoke and
> cfobject. I
> think my question is asked more accurately with an example, so here's a
> quick CFC and a couple different ways to call it that we can use
> as a basis
> of discussion..
>
> This is a very simple example of a situation where a programmer wants to
> select a USER record based on a user_id pk value.
>
> <!--- user.cfc --->
> <cfcomponent>
> <cffunction name="GetUser" returnType="Query" access="public">
> <cfargument name="user_id" type="numeric"
> required="yes" hint="Any
> USER.user_id value.">
>
> <cfinvoke method="Q_selectUser" returnVariable="Q_user">
> <cfinvokeargument name="user_id"
> value="#Arguments.user_id#">
> </cfinvoke>
>
> <cfreturn Q_user>
> </cffunction>
>
> <cffunction name="Q_selectUser" returnType="Query" access="private">
> <cfargument name="user_id" type="numeric"
> required="yes" hint="Any
> USER.user_id value.">
>
> <cfquery name="Q_user" datasource="#Request.dsn#">
> SELECT *
> FROM USER
> WHERE USER.user_id =
> <cfqueryparam value="#Arguments.user_id#"
> cfsqltype="cf_sql_integer">
> </cfquery>
>
> <cfreturn Q_user>
> </cffunction>
> </cfcomponent>
>
> There are two functions in that CFC..
>
> 1) GetUser (public function for templates to call)
> 2) Q_selectUser (private function that can be made more complex as more
> public functions are built that will use it)
>
>
>
> Now, a programmer that wants to use this module has a couple choices..
>
> 1) Use cfobject
>
> <cfobject action="Create" component="user" name="UserComponent">
> <cfset Q_user = UserComponent.GetUser(1)>
>
> 2) Use cfinvoke
>
> <cfinvoke method="GetUser" component="user" returnVariable="Q_user">
> <cfinvokeargument name="user_id" value="1">
> </cfinvoke>
>
>
>
> Now here are my questions..
>
> 1) Which one will perform better in a very high traffic environment?
> 2) Are there any significant disadvantages to either method?
> 3) If the same template was going to use more methods in the CFC, would
> there be extra parsing time necessary for subsequent cfinvoke calls?
>
>
>
> Thanks a ton for any comments you may have, it's always great to
> have a pool
> of resources to draw on for questions like this..
>
> - j.
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the word 'unsubscribe cfcdev'
in the message of the email.
CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).
An archive of the CFCDev list is available at www.mail-archive.com/[EMAIL PROTECTED]