<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:clearspace="http://www.jivesoftware.com/xmlns/jive/rss" version="2.0">
  <channel>
    <title>Adobe Community: Message List - Best Practices Question In Regards to Instancing Multiple CFCs That Need to Talk to Each Other</title>
    <link>https://forums.adobe.com/community/coldfusion?view=discussions</link>
    <description>Most recent forum messages</description>
    <language>en</language>
    <pubDate>Thu, 06 Nov 2014 17:36:54 GMT</pubDate>
    <generator>Jive Engage 7.0.0.1  (http://jivesoftware.com/products/)</generator>
    <dc:date>2014-11-06T17:36:54Z</dc:date>
    <dc:language>en</dc:language>
    <item>
      <title>Best Practices Question In Regards to Instancing Multiple CFCs That Need to Talk to Each Other</title>
      <link>https://forums.adobe.com/message/6905950?tstart=0#6905950</link>
      <description>&lt;!-- [DocumentBodyStart:eee2a9ce-fd76-4b12-9213-02d11e46afd7] --&gt;&lt;div class="jive-rendered-content"&gt;&lt;p&gt;Hello CF Fans,&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;I have a &amp;#8220;best-practices&amp;rdquo; question in regards to how you recommend instancing CFCs that all need to talk to each other in a given project.&lt;/p&gt;&lt;p&gt;Let&amp;#8217;s say for example you have a web application that has a bunch of different modules in it:&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Online Calendar&lt;/li&gt;&lt;li&gt;Online Store&lt;/li&gt;&lt;li&gt;Blog&lt;/li&gt;&lt;li&gt;File Manager (uploading/downloading/processing files)&lt;/li&gt;&lt;li&gt;User Accounts&lt;/li&gt;&lt;/ol&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Each of these modules is nicely organized so that the functions that pertain to each module are contained within separate CFC files:&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Calendar.cfc&lt;/li&gt;&lt;li&gt;Store.cfc&lt;/li&gt;&lt;li&gt;Blog.cfc&lt;/li&gt;&lt;li&gt;Files.cfc&lt;/li&gt;&lt;li&gt;Users.cfc&lt;/li&gt;&lt;/ol&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;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&amp;hellip;&lt;/p&gt;&lt;p&gt;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).&amp;nbsp; There are a couple ways that I&amp;#8217;ve been playing with accomplishing this:&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Within a CFC, instance the other CFC&amp;#8217;s that you&amp;#8217;re going to need:&lt;/li&gt;&lt;/ol&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span style="color: #ff6600;"&gt;&amp;lt;!--- Store.cfc ---&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style="color: #ff6600;"&gt; &amp;lt;cfcomponent&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span style="color: #ff6600;"&gt;&amp;lt;!--- instance all the CFC&amp;#8217;s we will need here ---&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style="color: #ff6600;"&gt; &amp;lt;cfset usersCFC = CreateObject(&amp;#8220;component&amp;rdquo;,&amp;rdquo;users&amp;rdquo;) /&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style="color: #ff6600;"&gt; &amp;lt;cfset filesCFC = CreateObject(&amp;#8220;component&amp;rdquo;,&amp;rdquo;files&amp;rdquo;) /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span style="color: #ff6600;"&gt;&amp;lt;cffunction name=&amp;rdquo;storeAction&amp;rdquo;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;&lt;p&gt;&lt;span style="color: #ff6600;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;cfset var customerInfo = usersCFC.getUser(1) /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;This approach seems to work for the most part unless some of the instanced CFC&amp;#8217;s also instance the CFC&amp;#8217;s that instance them.&amp;nbsp; For example: If Users.cfc instances Files.cfc and Files.cfc also instances Users.cfc. I&amp;#8217;ve run into problems with occasional dreaded NULL NULL errors with this probably because of some type of infinite recursion issue.&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 10pt; line-height: 1.5em;"&gt;&lt;br/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 10pt; line-height: 1.5em;"&gt;2. Another way I&amp;#8217;ve tried to approach this is to instance any needed CFCs inside a CFC&amp;#8217;s function scope (this seems to prevent the recursion issues):&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br/&gt; &lt;span style="color: #ff6600;"&gt;&amp;lt;!--- Store.cfc ---&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style="color: #ff6600;"&gt; &amp;lt;cfcomponent&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span style="color: #ff6600;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;cffunction name=&amp;rdquo;storeAction&amp;rdquo;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span style="color: #ff6600;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;!--- create a struct to keep all this function&amp;#8217;s variables ---&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style="color: #ff6600;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;cfset var local = structNew() /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;&lt;p&gt;&lt;span style="color: #ff6600;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;!--- instance all the CFC&amp;#8217;s we will need here ---&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style="color: #ff6600;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;cfset local.usersCFC = CreateObject(&amp;#8220;component&amp;rdquo;,&amp;rdquo;users&amp;rdquo;) /&amp;gt;&lt;/span&gt;&lt;br/&gt;&lt;span style="color: #ff6600;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;cfset local.filesCFC = CreateObject(&amp;#8220;component&amp;rdquo;,&amp;rdquo;files&amp;rdquo;) /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;&lt;p&gt;&lt;span style="color: #ff6600;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;cfset var customerInfo = local.usersCFC.getUser(1) /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;My concern with this approach is that it may not be as efficient because you wind up instancing the same CFC&amp;#8217;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.&lt;br/&gt; &lt;br/&gt; One thing I thought of, based on some things I&amp;#8217;ve seen online and articles on object oriented programming, is taking advantage of a &amp;#8220;Base.cfc&amp;rdquo; which uses the &amp;#8220;extends&amp;rdquo; property to instance all of the CFC&amp;#8217;s in the application.&amp;nbsp; However, I&amp;#8217;ve never tested this type of setup before and I&amp;#8217;m not sure if this is the ideal way to allow all my CFC&amp;#8217;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. &amp;#8220;init()&amp;rdquo;).&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;&lt;p&gt;&lt;span style="color: #ff6600;"&gt;&amp;lt;!--- Base.cfc ---&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: #ff6600;"&gt;&amp;lt;cfcomponent extends=&amp;rdquo;calendar store blog users files&amp;rdquo;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;How do you tackle this type of setup in your own applications?&amp;nbsp; I&amp;#8217;d love to hear your thoughts!&lt;/p&gt;&lt;/div&gt;&lt;!-- [DocumentBodyEnd:eee2a9ce-fd76-4b12-9213-02d11e46afd7] --&gt;&lt;img src='/beacon?t=1415918467688' /&gt;</description>
      <pubDate>Thu, 06 Nov 2014 17:36:54 GMT</pubDate>
      <author>forums_noreply@adobe.com</author>
      <guid>https://forums.adobe.com/message/6905950?tstart=0#6905950</guid>
      <dc:date>2014-11-06T17:36:54Z</dc:date>
      <clearspace:dateToText>1 week 5 hours ago</clearspace:dateToText>
      <clearspace:objectType>0</clearspace:objectType>
    </item>
  </channel>
</rss>

