On Thursday, Jul 31, 2003, at 07:33 US/Pacific, Tim Blair wrote:
The objectstore is basically an object cache for "static" objects that
goes along the lines of a singleton pattern -- only one instance of each
object is stored in the cache (the application scope).

But if your objectStore instance is already stored in application scope, then any objects it creates and stores in its own instance data are automatically stored in application scope (without breaking encapsulation).


objectStore (which is also stored in the application scope).  Basically
- yes, it does break encapsulation, but I don't see any other way of
doing it?

Simple - store the instances *inside* the objectStore:


        <cfcomponent>
                <cfset mycache = structNew() />
                <cffunction name="getFooBar" returntype="foo.bar">
                        <cfif not structKeyExists(mycache,"foobar")>
                                <cflock name="cached_foobar" type="exclusive">
                                        <cfif not structKeyExists(mycache,"foobar")>
                                                <cfset mycache.foobar = 
createObject("component","foo.bar") />
                                        </cfif>
                                </cflock>
                        </cfif>
                        <cfreturn mycache.foobar />
                </cffunction>
                ...
        </cfcomponent>

You don't need to reference application scope at all inside objectStore. It stores objects inside itself. It's already stored in application scope.

Hmm, that sounds sensible but doesn't get around the page context bug
does it?  Because the objectStore is held in the application scope, it
means that all references to it from another object will be to the
instance in the "other" object's original context?

I'm not sure quite what you mean here - what "references to it from another object"? The page context bug means that if you have a CFC instance in a shared scope, *that* CFC instance cannot reference shared scopes on subsequent requests (a workaround is to store a local reference to the shared scope when the instance is created - thanx to Nathan Dintenfass for that gem). I don't see how the page context bug affects the situation you are talking about.


Along with this, as a general point, if I create instances and put them
in a shared scope and they reference other objects in a shared scope, is
there in effect a copy of the objects for each of the "page contexts"?

No, because the objects are accessed by *reference* - they're not copied.


If an object in a shared scope needs to reference other objects in
the same (or other) shared scopes, it's probably better practice to
'register' those objects with it after creation than have it 'know'
about their existence in other shared scopes.
Er, huh? :)

Like this:


        <!--- child.cfc --->
        <cfcomponent>
                <cfset parent = 0 />
                <cffunction name="init">
                        <cfargument name="myParent">
                        <cfset parent = arguments.myParent />
                </cffunction>
                ...
        </cfcomponent>

        <!--- parent.cfc --->
        ...
                <cfset newChild = createObject("component","child") />
                <!--- register parent with child: --->
                <cfset newChild.init( this ) />
        ...

The child object now knows who its parent is and can call methods on it - without knowing anything about where the parent object is stored. So, no references to shared scopes.

Sean A Corfield -- http://www.corfield.org/blog/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the word 'unsubscribe cfcdev' in the message of the email.


CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

Reply via email to