Sent: Friday, October 03, 2003 12:45
PM
Subject: RE: [CFCDev] Just starting
CFC's -- Session Question
Yes --
basically, the cart should be self-contained in its logic. It
contains, as instance data, information about what's in the cart. It
has methods for manipulating that information and would expose methods that
would probably be something like:
addItem(item,quantity);
deleteItem(item);
changeQuantity(item,newQuantity);
getAllItems();
getSubTotal(); [add all prices of the items]
getAllItems might return a struct of structs, or it might return an
array of other CFC instances (instances of Item, perhaps?) -- that will
depend on your implementation, but the point I'm trying to make is that the
cart itself should not be "aware" of a session (in fact, your problem of
needing a CFAPPLICATION tag is a classic example of why a component should
minimize dependencies on the external environment). Thus, you
shouldn't ever need to pass the Session scope "into" the CFC (or out of the
CFC).
You can
just say things like:
session.cart.addItem(item,quantity);
myCartItems = session.cart.getAllItems();
or,
maybe something like:
for(item
in session.cart.getAllItemKeys()){
thisItem =
session.cart.getItem(item);
}
etc.
That makes good sense ... so basically build
the "cart" in the CFC and return to the the CFM template to update the
cartData session. Keep session information out of the CFC
completly?
I would assume you would also set the session to a local variable
before it went into the CFC.
How do you handle multiple structs and arrays being passed to a
component ..
Ie, I have cartData (individual items in the cart) and then orderData
that is all the information about the cart. They interact together
(maybe I need to think more object oriented).
Paul Giesenhagen
QuillDesign
----- Original Message -----
Sent: Friday, October 03, 2003
11:21 AM
Subject: RE: [CFCDev] Just starting
CFC's -- Session Question
I
have to throw in the obligatory and oft repeated advice that you should
not be directly manipulating session variables inside your CFCs,
generally speaking. You'd be better off having your cartModule
component store things as instance variables, then store an instance of
the cartModule in the session. This way you preserve encapsulation
because the cartModule does not need to be aware of its environment (you
could, for instance, use it in the Application or Server scope if that
were more appropriate in another setting). You also avoid any risk
of overwriting the data inside the cart. You also don't need to be
aware of how the cartModule is implemented to use it -- you can then use
CFCs' strength, which is to encapsulate complexity and expose
functionality through an API defined by the methods. Then if you
ever need to change how a cartModule works internally your other code
doesn't need to change because the API of the cartModule can stay the
same.
I am just now starting my learning on
CFC's and having issues with sessions .. it is quite
frustrating.
I created a VERY simple little input for
building an array populated with a struct and then sticking it into a
session.
I am not getting any session information
from the calling CFM page .. if I set the session inside of the
CFC.
Can anyone see what I am doing
wrong?
If I run cart.cfm?mode=addCart (I
see the session set in the CFC -- cfdump)
AND then Run cart.cfm?mode=viewCart it
says cartData is undefined in SESSION.
<!--- Calling CFM page
--->
<cfparam name="url.mode"
default="">
<cfswitch
_expression_="#url.mode#">
<cfcase
value="delCart">
<cfset variables.foo =
StructDelete(session,
"cartData")>
</cfcase>
<cfcase
value="addCart">
<cfparam name="url.qty"
default="1">
<cfparam name="url.id"
default="1">
<!--- <cfobject type="component"
name="addCart" component="test.components.cartModule">
--->
<cfinvoke
component="test.components.cartModule"
method="addCart"/>
</cfcase>
<cfcase
value="viewCart">
<cfdump
var="#session.cartData#">
</cfcase>
</cfswitch>
<!--- cartModule.cfc
--->
<cfcomponent displayname="cartModule"
hint="manages Items to the Cart">
<cffunction
name="addCart" output="true" displayname="Adds Cart Stuff" hint="Adds
Cart Stuff">
<cfif
IsDefined("session.cartData")>
<cfset
cartLen = ArrayLen(cart) + 1>
<cfset
cart[cartLen] = structNew()>
<cfset
cart[cartLen].qty = url.qty>
<cfset
cart[cartLen].id =
url.id>
<cfelse>
<cfset
cart = arrayNew(1)>
<cfset cartLen =
ArrayLen(cart) + 1>
<cfset cart[cartLen] =
structNew()>
<cfset cart[cartLen].qty =
url.qty>
<cfset cart[cartLen].id =
url.id>
</cfif>
<cfset
session.cartData = cart>
<cfdump
var="#session.cartData#">
</cffunction>
</cfcomponent>
Also: If I call this component via
CFOBJECT the <CFDUMP in the function doesn't display anything -- If
I use <CFINVOKE it displays the dump.
Thanks
Paul Giesenhagen
QuillDesign