Skip to main content
Participant
December 28, 2007
Answered

Providing Arguments for a entire component

  • December 28, 2007
  • 5 replies
  • 591 views
I am new to the <cfcomponent> game, but while writing my first component I find myself coding the same arguments for many functions. How do I go about coding my arguments once and re-use them within many functions. Do I create a function at the beginning and set the access to private and my returntype to struct? If so, does the structure store the names as Arguments.name and does the query use the same syntax (#Arguments.name#)?

Any thoughts, resources or examples would be most appreciated.
This topic has been closed for replies.
Correct answer Newsgroup_User
First of all, the required="true" and default paramterers are logically
mutually exclusive. If the argument is required a value must be passed
in the function call. If a value is provided in the function call the
default value is not use.

Second of all, it is considered poor practice to have CFC's directly
access exterior scopes such as "session". One reason is because it is
easy for a CFC file to live in a location such that it is not associated
with the exterior scope and it will not have access to the data. It is
considered a better practice to always pass in all the data a function
needs through arguments.

Finally, you may be able to make use of an 'init' function or a similar
process that defines these values for the entire cfc and then all
functions in it can access this data.

<cffunction name="init" ...>
<cfargument name="customer_nbr" required="yes" ...>
<cfargument name="store_nbr" required="yes" ...>

<cfset variables.customer_nbr = arguments.customer_nbr>
<cfset variables.store_nbr = arguments.store_nbr>
<!--- the 'variables' scope is available to all functions inside
this cfc. The 'this' scope is as well but it is also available to code
external to the cfc. --->

<cfreturn this>
</cffunction>

<cffunction name="storeMonthlyPurchase"...>

Use #variables.customer_nbr# and #variables.store_nbr# as required.

</cffunction>


This will require one to persist a CFC object from one function call to
another or to always call the init() function with the desired function
call. The <cfreturn this> line in the 'init' function facilitates this.

5 replies

Newsgroup_UserCorrect answer
Inspiring
December 28, 2007
First of all, the required="true" and default paramterers are logically
mutually exclusive. If the argument is required a value must be passed
in the function call. If a value is provided in the function call the
default value is not use.

Second of all, it is considered poor practice to have CFC's directly
access exterior scopes such as "session". One reason is because it is
easy for a CFC file to live in a location such that it is not associated
with the exterior scope and it will not have access to the data. It is
considered a better practice to always pass in all the data a function
needs through arguments.

Finally, you may be able to make use of an 'init' function or a similar
process that defines these values for the entire cfc and then all
functions in it can access this data.

<cffunction name="init" ...>
<cfargument name="customer_nbr" required="yes" ...>
<cfargument name="store_nbr" required="yes" ...>

<cfset variables.customer_nbr = arguments.customer_nbr>
<cfset variables.store_nbr = arguments.store_nbr>
<!--- the 'variables' scope is available to all functions inside
this cfc. The 'this' scope is as well but it is also available to code
external to the cfc. --->

<cfreturn this>
</cffunction>

<cffunction name="storeMonthlyPurchase"...>

Use #variables.customer_nbr# and #variables.store_nbr# as required.

</cffunction>


This will require one to persist a CFC object from one function call to
another or to always call the init() function with the desired function
call. The <cfreturn this> line in the 'init' function facilitates this.
Participant
December 28, 2007
I am not trying to factor out the <cfargument> tag, I am just using the same arguments over and over again in different <cffunction>'s

<!---storeMonthlyPurchases() method get results for a specific customer and store--->
<cffunction name="storeMonthlyPurchases" output="false" access="public" returntype="query">
<!---Required Arguments to make the method work--->
<cfargument name="customer_nbr" required="yes" default="#Session.kt_customer_nbr#">
<cfargument name="store_nbr" required="yes" default="#Session.kt_store_nbr#">

Inspiring
December 28, 2007
quote:

Originally posted by: gman593
I am new to the <cfcomponent> game, but while writing my first component I find myself coding the same arguments for many functions. How do I go about coding my arguments once and re-use them within many functions. Do I create a function at the beginning and set the access to private and my returntype to struct? If so, does the structure store the names as Arguments.name and does the query use the same syntax (#Arguments.name#)?

Any thoughts, resources or examples would be most appreciated.

I use an init function.

When I was a cfc rookie, I converted everything to the Property scope. It wasn't necessarily the best method, but it worked.

I've also converted everything to the "this" scope. Once again, it works.

A limitation of the two methods above is that you can't use a cfinvoke tag. You have to create an object and run the functions one by one.

Something else I've done that works is to declare a bunch of variables immediately after your cfcomponent tag and then use the Init function to process the arguments. Somewhere in all the other functions, is something like this:
<cfif WhatToUse is "not set">
<cfset x = this.init(argumentcollection = arguments)>
</cfif>

There might be better methods out there, but these all work.
Inspiring
December 28, 2007
To look at this in a different way then Adam did. He is correct in that
there is no real way to reuse arguments. But if you have function after
function using the same data, maybe you can set that data as instance
data of the component where it is available to all methods. Then you
just have a|several method(s) for setting the instance data.

As Adam suggested a brief outline of the component would allow others to
help you with more specific suggestions.
Inspiring
December 28, 2007
> I am new to the <cfcomponent> game, but while writing my first component I find
> myself coding the same arguments for many functions. How do I go about coding
> my arguments once and re-use them within many functions.

You don't.

It's the nature of the beast that methods within a component will have
similar arguments quite often. But one still ought define the arguments
for each method.

Can you post a cut down version of your CFC (just the <cffunction> and
<cfargument> tags should suffice). I'm puzzled at why there's so much
duplication you're seeing so as to be worth trying to factor out the
<cfargument> tags.

--
Adam