Why would you return 'this' from the init function?  'This' is not a pointer
to the object, so what good does it do the method caller?

-----Original Message-----
From: Nathan Dintenfass [mailto:[EMAIL PROTECTED]
Sent: Thursday, August 14, 2003 6:40 PM
To: [EMAIL PROTECTED]
Subject: RE: [CFCDev] CFC Persistance


Sean's concise answer may be all you need to know, but just in case you
wanted more....

In general, CFC caching is very much like all variable-based caching in CF.
At your disposal you have the Application, Session, and Server scopes.
Which one you use will depend on the nature of the component in question.

So, for instance, take a very simple shopping cart component.  I could build
the component to store, as instance data, the contents of a cart.  Perhaps
this is as simple as a structure of structures, or perhaps it's a structure
of other components -- but, that's a detail not germane  to this discussion.
Your shopping cart might be something like:

<cfcomponent>

<cffunction name="init....>
<cfscript>
variables.cartItems = structNew();
return this;
</cfscript>
</cffunction>

<cffunction name="addItem"....>
<cfargument name="itemID"...>
<cfargument name="amount" default="1"....>
<cfset variables.cartItems[arguments.itemID] = newCartEntry(itemID,amount)>
</cffunction>

<cffunction name="newCartEntry"...>
<cfargument name="itemID"...>
<cfargument name="amount"...>
<cfset var thisEntry = structNew()>
<cfscript>
[NOTE -- this might be where you get item info from a DB instead of just the
ID]
thisEntry.item = structNew();
thisEntry.item.id = arguments.itemID;
thisEntry.amount = arguments.amount;
</cfscript>
</cffunction>

<cffunction name="getAllItems"...>
<cfreturn varibles.cartItems>
</cffunction>

.....

</cfcomponent>

Then, you could give each session a cart:

<cfparam name="session.cart"
default="#createObject("component","shoppingCart").init()#">

So, you now have a shoppingCart instance cached in the session scope, and
you can use the methods of that instance to do all things cart related
(obviously, the above example is lacking a lot of functionality your real
cart would have) while storing instance data as part of itself.

Thus, the component knows nothing about its environment (good encapsulation)
and can be used any way someone sees fit.  Meanwhile, your application
decides the best way to use the cart is to give each session its own cart.
The cart is good at storing/manipulating/giving cart stuff and your
application is good at using a cart.  All is right with the world ;)

Hope this answers your question.









> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Behalf Of John Farrar
> Sent: Thursday, August 14, 2003 4:12 PM
> To: [EMAIL PROTECTED]
> Subject: [CFCDev] CFC Persistance
>
>
> There has been talk about CFC caching... persistance... ect. Can
> some of the
> GURUs share with us how and why they achieve this? Reading accross the
> threads I have an idea how to do it... but am aware that we might miss
> something in the fray of multiple posts.
>
> Example: Shopping Cart... I want it to persist without having to hit the
> database each time. So, do I create the object each time in
> "request" scope,
> and then pull the variable set from somewhere? How are people
> doing this and
> what is the performance gain, coding advantage... please feel generous and
> bless those who will follow in your footsteps.
>
> John Farrar

----------------------------------------------------------
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).

----------------------------------------------------------
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