Best Practice for calling CFC
Hi,
In a web application, if I need to call a CFC method from a different CFC, what would be considered as the best way of doing it?
For example, let's say I have two components: Customer and Product. From a method functionA in Customer, I would like to call functionB in Product. I can do one of the following, but which way is best practice and why?
1. Create a Product object in functionA, and use it to call functionB
<cfcomponent name="Customer">
<cffunction name="functionA">
<cfset productObj = createObject('component', 'Product')>
<cfset productObj.functionB()>
</cffunction>
</cfcomponent>
2. Pass a Product object when we initialize a Customer object, and use that to call functionB
<cfcomponent name="Customer">
<cffunction name="init">
<cfargument name="productObj">
<cfset variables.productObj = arguments.productObj>
</cffunction>
<cffunction name="functionA">
<cfset variables.productObj.functionB()>
</cffunction>
</cfcomponent>
3. Assume that Customer object has access to the Product object in the application scope
<cfcomponent name="Customer">
<cffunction name="functionA">
<cfset application.productObj.functionB()>
</cffunction>
</cfcomponent>
Thank you very much.
