|
Mailing Lists
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
does this violate good form for components?
Author: Michael Hodgdon
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32800#164560
Thanks Jim, I will look into some of those compiling issues and go forward with
our model. It works for us so far. The time we save in development is
definitely worth it, it became very hard to read our CFC's as we had so much code
in them.
Thanks again and to all that posted
Michale
----- Excess quoted text cut - see Original Post for more -----
Author: Jim Davis
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32800#164553
My bad - I think that you're correct in that - what you have looks like it
should work.
I don't know how it works under the covers tho' - in CFMX includes are
compiled separately (which is why they have to follow tag consistency and
nesting rules inside themselves). I'm not sure how CFMX is handling
"remote" method calls like this. it might hurt performance.
Also I'm not sure how good CFMX is above recompiling CFCs when a CFINCLUDED
part of them has been updated. I don't think there's a problem there, but
you might want to test it out.
I don't know if Macromedia has addressed the scoping issue officially in any
way (but since they use CFINCLUDING function bodies as an example in the CFC
documentation I really hope they do!)
Jim Davis
_____
Sent: Wednesday, May 26, 2004 3:24 PM
To: CF-Talk
Subject: Re: does this violate good form for components?
>That's why I include the function and not the contents of the function
>into the component.
>
>Jim Davis wrote:
>
>> I recently had to rewrite a ton of my code to deal with this issue.
Hi Jim, we had heard that about including inside a function. The code that
I showed to you however does not include the contents of the function, it is
an include of the whole function. So one of those cfincludes would have
<cffunction ... arguments ...>
... code ....
</cffunction>
does this nasty little var scope / cfinclude issue happen whenever you use
cfinclude on a cfc ANYWHERE or just when you use cfinclude inside a FUNCTION
itself?
Also, do you happen to have any reference to tech notes from macromedia?
Thanks a bunch.
Michael
_____
Author: Jim Davis
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32800#164541
_____
Sent: Wednesday, May 26, 2004 3:10 PM
To: CF-Talk
Subject: Re: does this violate good form for components?
That's why I include the function and not the contents of the function
into the component.
Oops - my bad.
Jim Davis
Author: Michael Hodgdon
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32800#164536
>That's why I include the function and not the contents of the function
>into the component.
>
>Jim Davis wrote:
>
>> I recently had to rewrite a ton of my code to deal with this issue.
Hi Jim, we had heard that about including inside a function. The code that I
showed to you however does not include the contents of the function, it is an
include of the whole function. So one of those cfincludes would have
<cffunction ... arguments ...>
... code ....
</cffunction>
does this nasty little var scope / cfinclude issue happen whenever you use
cfinclude on a cfc ANYWHERE or just when you use cfinclude inside a FUNCTION
itself?
Also, do you happen to have any reference to tech notes from macromedia?
Thanks a bunch.
Michael
Author: Bryan F. Hogan
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32800#164528
That's why I include the function and not the contents of the function
into the component.
Jim Davis wrote:
----- Excess quoted text cut - see Original Post for more -----
Author: Jim Davis
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32800#164525
As for CFINCLUDING into a component I would highly recommend against it.
There is a nasty little "feature" to that: when you CFINCLUDE inside a
component method CF moves all of your unnamed scope (function local)
variables into the variables scope of the CFC.
This effectively blows threads safety out of the water and completely
invalidates any proper use of "var".
I recently had to rewrite a ton of my code to deal with this issue.
Jim Davis
Author: Bryan F. Hogan
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32800#164511
Here is what I do.
1. d:\wwwroot\components setup a mapping to this directory called
components or something.
2. each website has it's own folder for components under this directory.
For example.
d:\wwwroot\components\myweb1
3. In d:\wwwroot\components there is also a file that is used by all
applications for my udfs. cfcremotedocumenter.cfm is also in this directory.
d:\wwwroot\components\udfs.cfc
4. Inside that folder I have one folder and two files. The first file is
a config.xml file. The second is a factory.cfc.
5. The config.xml file contains variables that you would normally setup
in your application.cfm file.
<?xml version="1.0" encoding="UTF-8"?>
<config>
<dsn>yourwebsitedsn</dsn>
<mapping>/yourwebsitemapping/</mapping>
<baseURL>http://www.mywebsite.com/</baseURL>
<mailServer>mail.myserver.com</mailServer>
</config>
6. The factory.cfc contains the following.
<cfcomponent extends="components.udfs">
<!---
**
* Includes file that documents this component.
*
**
--->
<cfset instance.documentedAccessLevels='private,package,public,remote'>
<cfinclude template="../cfcremotedocumenter.cfm">
<!---
**
* Initializes local variables used within this component.
**
--->
<cfset init()>
<cfdirectory action="list"
directory="#getDirectoryFromPath(getCurrentTemplatePath())&'function_includes\'#"
filter="_*_functions.cfm"
name="qry_getFunctionIncludes">
<cffunction name="init" access="public" returntype="struct" output="false"
hint="Global Variables used within component.">
<cfset var initReturn=structNew()>
<cfset var objXML=''>
<cfset var idx=1>
<cffile action="read" charset="utf-8"
file="#replaceNoCase(getCurrentTemplatePath(),
listLast(getCurrentTemplatePath(), '\'), 'config.xml')#"
variable="objXML">
<cfset objXML=xmlParse(objXML)>
<cfset objXML=xmlSearch(objXML, '/config/')>
<cfloop from="1" to="#arrayLen(objXML[1].xmlChildren)#" index="idx">
<cfset
initReturn[objXML[1].xmlChildren[idx].xmlName]=objXML[1].xmlChildren[idx].xmlText>
</cfloop>
<cfreturn initReturn>
</cffunction>
<cffunction name="getVars" access="public" returntype="any" output="false"
hint="Returns the global vars initialized in the init() function.">
<cfset var getVarsReturn=init()>
<cfreturn getVarsReturn>
</cffunction>
<cfoutput query="qry_getFunctionIncludes">
<cfinclude template="function_includes/#name#">
</cfoutput>
</cfcomponent>
7. There is a function_includes for each application under
d:\wwwroot\components\myweb1\function_includes
8. Inside that folder I have all my functions. I split the files up into
logical function sets and make them a cfml page that is included by
factory.cfc. For example:
_calendar_functions.cfm
_document_functions.cfm
9. In the root Application.cfm file I have the following.
<cfsilent>
<cfapplication name="myweb1" clientmanagement="yes"
sessionmanagement="yes" sessiontimeout="#createTimeSpan(0,0,20,0)#"
applicationtimeout="#createTimeSpan(0,0,20,0)#" loginstorage="session"
setdomaincookies="yes">
<cfscript>
if(not isDefined('application.factory') or isDefined('url.reinit')) {
application.factory=createObject('component',
'components.myweb1.factory');
}
request.factory=application.factory;
</cfscript>
</cfsilent>
10. Now you have access to any function in any page of your application.
<cfdump
var="#request.factory#">
Author: Michael Hodgdon
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32800#164494
As we my development team has started coding our web sites using cfcomponents, we
have found that our component libraries have become extremely long.
We have broken down our model into as many cfc's as we can. So for example one
of our sites may have website, customer, product, appmodule, etc... cfc's. And
in those cfc's our function library starting growing fairly large and finding
functions was getting overwhelming.
So, we starting using <cfinclude> to include those functions, however, we
are wondering if this is poor form and if we are breaking any major rules with
this. Our lives have become much simpler within our web sites, but if this is
discouraged due to securiy or OO design, we would willingly change.
Any feedback:
CODE EXAMPLE:
<cfcomponent displayname="customer" extends="cf_components/user" hint="I
represent a customer. My functions support actions that a user takes while
utilizing the application.">
<cffunction name="initializeObject" access="public" output="yes"
returntype="any">
<!--- Attempt to run the function code --->
<cftry>
<!--- set some default variables for the customer--->
<cfscript>
... various init routines ....
</cfscript>
<!--- If something went wrong with the function code... --->
<cfcatch type="Any">
<!--- Run the error handling routine --->
<cfinclude template="/cf_includes/cfcatch.cfm">
<cfreturn "1">
</cfcatch>
</cftry>
</cffunction>
<!--- Methods to access and manipulate customer object information --->
<cfinclude template="customer/method1.cfm">
<cfinclude template="customer/method2.cfm">
<cfinclude template="customer/method3.cfm">
<cfinclude template="customer/method4.cfm">
<cfinclude template="customer/method5.cfm">
.... more functions would be included .....
</cfcomponent>
|
May 24, 2012
|
Latest Fusion Authority Articles
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||