Should I then always create dependent objects in the Factory and then pass
them into the CFC? Or should I use the factory in the CFC to create the
object. Examples:
EXAMPLE A (CREATE DEPENDENT OBJECT INSIDE CFC)
**Factory.cfc**
<cffunction name="makeXXX" returntype="XXX">
<cfset var XXXObj = createObject('component','XXX').init(this) />
<cfreturn XXXObj />
</cffunction>
**XXX.cfc**
<cffunction name="init" returntype="XXX">
<cfargument name="Factory" />
<cfset variables.instance=structnew() />
<cfset variables.instance.Factory=arguments.Factory />
<cfset variables.instance.LTO= variables.instance.Factory.load('XXXLTO')>
<cfreturn this />
</cffunction>
OR EXAMPLE B (CREATE DEPENDENT OBJECT FIRST IN FACTORY)
<cffunction name="makeXXX" returntype="XXX">
<cfset var LTO = load('XXXLTO') />
<cfset var XXXObj = createObject('component','XXX').init(this,LTO) />
<cfreturn XXXObj />
</cffunction>
**XXX.cfc**
<cffunction name="init" returntype="XXX">
<cfargument name="Factory" />
<cfargument name="LTO" />
<cfset variables.instance=structnew() />
<cfset variables.instance.Factory=arguments.Factory />
<cfset variables.instance.LTO= arguments.LTO>
<cfreturn this />
</cffunction>
Cheers,
Baz
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Nando
Sent: Monday, October 31, 2005 8:08 AM
To: [email protected]
Subject: RE: [CFCDev] Global Function Libraries
>Also, what if CFCs need to create objects, how do they call the Factory if
>its in the app scope?
:)
Isn't this stuff great to think about?
In the previous example, let's say your ServiceObj needed Factory to
createObject()'s for it.
<cffunction name="makeServiceObj" access="private" returntype="ServiceObj"
output="no">
<cfset var ServiceObj =
createObject('component','ServiceObj').init(variables.Utils, this) />
<cfreturn ServiceObj />
</cffunction>
That's all you'd need to do! Well, almost ... Did you notice the difference?
:)
Then in the init() of ServiceObj, all you'd need is this:
<cfset variables.Factory = arguments.Factory />
Of course, once you've got a reference to Factory in your ServiceObj, then
you wouldn't need necessarily to pass Utils into it. You could just ask
Factory for a instance:
In Factory:
<cffunction name="makeServiceObj" access="private" returntype="ServiceObj"
output="no">
<cfset var ServiceObj =
createObject('component','ServiceObj').init(this)
/>
<cfreturn ServiceObj />
</cffunction>
Then in the init() of ServiceObj, all you'd need is this:
<cfset variables.Factory = arguments.Factory />
<cfset variables.Utils = variables.Factory.load('Utils') />
None of these examples are "perfect" ... it all really depends on the
context of your application and your skill and preferences as a developer.
The point is that we're headed down a different path, using some OO
principles to create a more robust, flexible app. What just happened was a
requirements change. You suddenly needed the capability to createObject()
within some ServiceObj. And you got it with no change to Factory's public
interface, load(), and a very minor change to makeServiceObj() and
ServiceObj.init()
If you find yourself battling with "scopes" - you haven't come up with the
right architecture yet!
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to
[email protected] with the words 'unsubscribe cfcdev' as the subject of the
email.
CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting
(www.cfxhosting.com).
An archive of the CFCDev list is available at
www.mail-archive.com/[email protected]
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to
[email protected] with the words 'unsubscribe cfcdev' as the subject of the
email.
CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting
(www.cfxhosting.com).
An archive of the CFCDev list is available at
www.mail-archive.com/[email protected]