Highlighted

Best Practices Question In Regards to Instancing Multiple CFCs That Need to Talk to Each Other

Participant ,
Nov 06, 2014

Copy link to clipboard

Copied

Hello CF Fans,

I have a “best-practices” question in regards to how you recommend instancing CFCs that all need to talk to each other in a given project.

Let’s say for example you have a web application that has a bunch of different modules in it:

  1. Online Calendar
  2. Online Store
  3. Blog
  4. File Manager (uploading/downloading/processing files)
  5. User Accounts

Each of these modules is nicely organized so that the functions that pertain to each module are contained within separate CFC files:

  1. Calendar.cfc
  2. Store.cfc
  3. Blog.cfc
  4. Files.cfc
  5. Users.cfc

Each CFC contains functions appropriate for that particular module. For example, the Users.cfc contains functions pertaining to logging users on/off, updating account info etc…

Sometimes a CFC might need to reference a function in another CFC, for example, if the store (Store.cfc) needs to get information from a customer (Users.cfc).  There are a couple ways that I’ve been playing with accomplishing this:

  1. Within a CFC, instance the other CFC’s that you’re going to need:

<!--- Store.cfc --->
<cfcomponent>

<!--- instance all the CFC’s we will need here --->
<cfset usersCFC = CreateObject(“component”,”users”) />
<cfset filesCFC = CreateObject(“component”,”files”) />

<cffunction name=”storeAction”>

              

     <cfset var customerInfo = usersCFC.getUser(1) />

This approach seems to work for the most part unless some of the instanced CFC’s also instance the CFC’s that instance them.  For example: If Users.cfc instances Files.cfc and Files.cfc also instances Users.cfc. I’ve run into problems with occasional dreaded NULL NULL errors with this probably because of some type of infinite recursion issue.


2. Another way I’ve tried to approach this is to instance any needed CFCs inside a CFC’s function scope (this seems to prevent the recursion issues):


<!--- Store.cfc --->
<cfcomponent>

     <cffunction name=”storeAction”>

          <!--- create a struct to keep all this function’s variables --->
           <cfset var local = structNew() />

         

          <!--- instance all the CFC’s we will need here --->
           <cfset local.usersCFC = CreateObject(“component”,”users”) />
           <cfset local.filesCFC = CreateObject(“component”,”files”) />

         

          <cfset var customerInfo = local.usersCFC.getUser(1) />

My concern with this approach is that it may not be as efficient because you wind up instancing the same CFC’s multiple times for each function that needs it but it does solve the problem of recursion by isolating the CFCs to their respective functions.

One thing I thought of, based on some things I’ve seen online and articles on object oriented programming, is taking advantage of a “Base.cfc” which uses the “extends” property to instance all of the CFC’s in the application.  However, I’ve never tested this type of setup before and I’m not sure if this is the ideal way to allow all my CFC’s to talk to each other especially since I believe using extends overwrites functions if any of them share a common function name (e.g. “init()”).

              

<!--- Base.cfc --->

<cfcomponent extends=”calendar store blog users files”>

How do you tackle this type of setup in your own applications?  I’d love to hear your thoughts!

Views

114

Likes

Translate

Translate

Report

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

Best Practices Question In Regards to Instancing Multiple CFCs That Need to Talk to Each Other

Participant ,
Nov 06, 2014

Copy link to clipboard

Copied

Hello CF Fans,

I have a “best-practices” question in regards to how you recommend instancing CFCs that all need to talk to each other in a given project.

Let’s say for example you have a web application that has a bunch of different modules in it:

  1. Online Calendar
  2. Online Store
  3. Blog
  4. File Manager (uploading/downloading/processing files)
  5. User Accounts

Each of these modules is nicely organized so that the functions that pertain to each module are contained within separate CFC files:

  1. Calendar.cfc
  2. Store.cfc
  3. Blog.cfc
  4. Files.cfc
  5. Users.cfc

Each CFC contains functions appropriate for that particular module. For example, the Users.cfc contains functions pertaining to logging users on/off, updating account info etc…

Sometimes a CFC might need to reference a function in another CFC, for example, if the store (Store.cfc) needs to get information from a customer (Users.cfc).  There are a couple ways that I’ve been playing with accomplishing this:

  1. Within a CFC, instance the other CFC’s that you’re going to need:

<!--- Store.cfc --->
<cfcomponent>

<!--- instance all the CFC’s we will need here --->
<cfset usersCFC = CreateObject(“component”,”users”) />
<cfset filesCFC = CreateObject(“component”,”files”) />

<cffunction name=”storeAction”>

              

     <cfset var customerInfo = usersCFC.getUser(1) />

This approach seems to work for the most part unless some of the instanced CFC’s also instance the CFC’s that instance them.  For example: If Users.cfc instances Files.cfc and Files.cfc also instances Users.cfc. I’ve run into problems with occasional dreaded NULL NULL errors with this probably because of some type of infinite recursion issue.


2. Another way I’ve tried to approach this is to instance any needed CFCs inside a CFC’s function scope (this seems to prevent the recursion issues):


<!--- Store.cfc --->
<cfcomponent>

     <cffunction name=”storeAction”>

          <!--- create a struct to keep all this function’s variables --->
           <cfset var local = structNew() />

         

          <!--- instance all the CFC’s we will need here --->
           <cfset local.usersCFC = CreateObject(“component”,”users”) />
           <cfset local.filesCFC = CreateObject(“component”,”files”) />

         

          <cfset var customerInfo = local.usersCFC.getUser(1) />

My concern with this approach is that it may not be as efficient because you wind up instancing the same CFC’s multiple times for each function that needs it but it does solve the problem of recursion by isolating the CFCs to their respective functions.

One thing I thought of, based on some things I’ve seen online and articles on object oriented programming, is taking advantage of a “Base.cfc” which uses the “extends” property to instance all of the CFC’s in the application.  However, I’ve never tested this type of setup before and I’m not sure if this is the ideal way to allow all my CFC’s to talk to each other especially since I believe using extends overwrites functions if any of them share a common function name (e.g. “init()”).

              

<!--- Base.cfc --->

<cfcomponent extends=”calendar store blog users files”>

How do you tackle this type of setup in your own applications?  I’d love to hear your thoughts!

Views

115

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 06, 2014 0

Have something to add?

Join the conversation