|
Hi Jay, and Paul,
I made a simple adjustment to my cfc browser file (
_component_cfcToHTML.cfm), so that the auto-generated documentation
also shows a "usage" section for invoking and
instantiating the component, there is also an "usage" section for each
method.
Probably a lot of people on the list have done
similar thing, but I consider it really helpfull, it saves me a lot of
typing.
If anyone wants to try it, I just uploaded
to:
Regards,
----- Original Message -----
Sent: Friday, October 03, 2003 4:42
PM
Subject: RE: [CFCDev] CFINVOKE vs.
CFOBJECT -- which way and why?
Hey Paul,
I've been wrestling with the same question lately and I've concluded
that you shouldn't paint your entire application with the same brush.
Meaning, it's really a case-by-case decision.
Here are some options for you:
<!--- option 1 --->
<cfscript>
myCustomerControllerInstance = CreateObject("component",
"customerController");
customerStruct =
myCustomerControllerInstance.getCustomer(checkCustomer.customerID);
</cfscript>
<!--- option 2 --->
<cfobject component="customerController"
name="myCustomerControllerInstance"/>
<cfinvoke method="getCustomer"
component="#myCustomerControllerInstance#"
customerId="#checkCustomer.customerID#"
returnVariable="customerStruct"/>
<!--- option 3 --->
<cfinvoke method="getCustomer"
component="customerController"
customerId="#checkCustomer.customerID#"
returnVariable="customerStruct"/>
<!--- option 4
--->
<cfinvoke method="getCustomer"
component="customerController"
returnVariable="customerStruct">
<cfinvokeargument
name="customerId" value="#checkCustomer.customerID#"/>
</cfinvoke>
Options 1 and 2 are instantiating a copy
of the component in your Variables scope. These are useful if you're
planning on using more than one method in the component during the request so
that you only have to hit the file system once.
Options 3 and 4 are hitting the file
system to read the component into memory. Sometimes this is appropriate,
and other times it isn't. It depends on a lot of factors.
I would only use option 4 if
you're going to optionally pass an argument. Option 3 is almost always
possible, and it's quicker to type and easier to read.
In my opinion, Option 1 is not really a
good idea since CF is not a strictly typed language. Future developers
will have to read through your CFCs to fully understand what you're doing if
your variable names aren't clear. I would only use this option for
methods that have 0 arguments.
So, what it boils down to for me, is
options 2 and 3. 1 and 4 are good to know about, and they're handy from
time to time, but generally they should be avoided unless you've got a really
good reason to use them.
Just my $0.02..
- j.
I am curious about the code below ... is there
a BETTER way to call this with CFOBJECt and if so, why?
Once I build the struct "customer" I am going
to stick it into a session named customer.
I am using CFINVOKE This way:
<!--- CFM Template --->
<cfinvoke
component="customerController"
method="getCustomer"
returnvariable="customer"> <cfinvokeargument
name="customerID"
value="#checkCustomer.customerID#"> </cfinvoke>
<!--- customerController.cfc
--->
<cfcomponent displayname="customer"
hint="Controls all access to customer information add, edit,
delete."> <cffunction name="getCustomer" output="false"
returntype="struct" displayname="Selects Customer" hint="Gets customer
information and populates cthe customer
structure."> <cfquery datasource="qdc2"
name="checkCustomer"> SELECT customerID, firstName,
lastName, email, company, address1,
address2, city, state, country, zip,
dayPhone, evePhone, mailList FROM
customer WHERE customerID = <cfqueryparam
value="#arguments.customerID#"
cfsqltype="CF_SQL_INTEGER"> </cfquery> <cfif
checkCustomer.recordcount> <cfset customer =
StructNew()> <cfset customer.customerID =
checkCustomer.customerID> <cfset
customer.firstName =
checkCustomer.firstName> <cfset customer.lastName
= checkCustomer.lastName> <cfset customer.company
= checkCustomer.company> <cfset customer.email =
checkCustomer.email> <cfset customer.address1 =
checkCustomer.address1> <cfset customer.address2
= checkCustomer.address2> <cfset customer.city =
checkCustomer.city> <cfset customer.state =
checkCustomer.state> <cfset customer.zip =
checkCustomer.zip> <cfset customer.country =
checkCustomer.country> <cfset customer.dayPhone =
checkCustomer.dayPhone> <cfset customer.evePhone
= checkCustomer.evePhone> <cfset
customer.mailList =
checkCustomer.mailList> <!---
Insert Shipping Information
---> <cfset customer.shipFirstName =
""> <cfset customer.shipLastName =
""> <cfset customer.shipCompany =
""> <cfset customer.shipAddress1 =
""> <cfset customer.shipAddress2 =
""> <cfset customer.shipCity =
""> <cfset customer.shipState =
""> <cfset customer.shipZip =
""> <cfset customer.shipCountry =
""> <cfset customer.shipDayPhone =
""> <cfelse> <!--- Do
something else
---> </cfif> <cfreturn
customer> </cffunction> </cfcomponent>
|