[Seaside] Object Instances / Garbage collection

Ramon Leon ramon.leon at allresnet.com
Thu Aug 31 19:24:26 UTC 2006


> I suspect this might be a general Squeak question, but I want 
> to ask it here in case Seaside adds some complications or 
> things to consider:
> 
> How do I delete object instances (is forcing garbage collection
> involved) from the image?
> 
> So, for instance, say I have a simple Shopping Cart and Items 
> sort of setup.
> I add several Items to the cart, which creates instances of 
> those objects.
> 
> And then I want to remove them from the cart, delete them. 
> But they're still around in the image, inspecting Item 
> allInstances still reveals those items which are no longer in 
> the cart.
> 
> This sort of thing happens all the time in my web apps (I'm 
> using the simplest form of persistence - in image, no 
> database or anything, while prototyping), and I'm wondering 
> how you guys get around that.
> 
> ~Dmitri

You shouldn't really force gc or flush the session caches, you really just
need to change your mindset.  Once you remove them from the cart, forget
about them, they're gone, they'll be gc'd eventually.  Don't use SomeClass
allInstances as if it were a database, it's not.  If you want to keep track
of all of your carts, or items, or whatever, put them in your own collection
or dictionary somewhere, so that when you remove them, they are effectively
deleted.  If you want in image persistence, try something like adding an
accessor on the class side like..

Order class>>allOrders
    ^allOrders ifNil: [allOrders := Dictionary new]

Then you can do stuff like

Transcript show: (Order allOrders at: someId)

Order allOrders do:[:each |
  html text: each asString; break].

Try to forget about the concept of delete, that's a relational concept, it
doesn't really exist in OO.  With transparent persistence, an object is
either referenced, or it isn't, that's all you have to think about.  If you
want to save an object, just put it somewhere like

Order allOrders at: someId put: Order new.

Now it's saved.  If you want to get rid of it, then remove it

Order allOrders removeKey: someId 

Or 

Order allOrders remove: anOrder

That's it, it's gone.  Don't worry that a transient reference might hang
around for a while, you shouldn't care, it's been removed from your model.

Or maybe you want to consider you SeasideSession as a database, put stuff on
the class side

MySession>>allOrders
    ^self class allOrders

MySession class>>allOrders
    ^allOrders ifNil: [allOrders := Dictionary new]

MySession>>allCustomers
    ^self class allCustomers

MySession class>>allCustomers
    ^allCustomers ifNil: [allCustomers := Dictionary new]

Then from you code it's

self session allOrders, or self session allCustomers.  Just a couple simple
ideas.



More information about the Seaside mailing list