Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Best Practice for calling CFC

Guest
Sep 13, 2010 Sep 13, 2010

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.

907
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 14, 2010 Sep 14, 2010

I would use Method 1 because it gives you the most flexiblility.  While your known current requirements might dictate that it will be necessary to call the product cfc 100% of the time you call the customer cfc, something might come up in the future when it's not.  Method 2 would then be inefficient because it's running unnecessary code.

Translate
LEGEND ,
Sep 13, 2010 Sep 13, 2010

The first two are fine.  If the CFC being called is always gonna be the exact same one, then there's no prob directly referencing it in the calling CFC.  If the CFC could vary, then pass it in.

If you're only using the CFC transiently, then you could use <cfinvoke> as well in this case.

Directly accessing an application-scoped CFC within a method is poor practice.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 14, 2010 Sep 14, 2010

I would use Method 1 because it gives you the most flexiblility.  While your known current requirements might dictate that it will be necessary to call the product cfc 100% of the time you call the customer cfc, something might come up in the future when it's not.  Method 2 would then be inefficient because it's running unnecessary code.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 14, 2010 Sep 14, 2010
LATEST

Thank you both for answering my question.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources