munging objects before persisting

Chris Muller chris at funkyobjects.org
Thu Sep 14 02:42:27 UTC 2006


> In the Pier-Magma integration there are instances od PRContext whose
> #properties instance var could really use zeroing to nil, before it
> is
> ever persisted.

Ok, no problem.  Ma object serialization provides a couple of
mechanisms for doing this.

> I had a look at asStorageObject, but this seems to need an object
> that
> has its own graph. I assume that this means that it is representable
> as
> a discrete entity, rather than as part of the overall objects graph.

Absolutely correct.  It should be a discrete entity, it should not link
to any object in the database graph.

A fine example of this is for persisting CompiledMethods.  See
MaCompiledMethodStorage.

> My current solution is to ensure that my 'dangerous' object is always
> sent #magmaCopy before it is stored in the persistent model, and also
> ensure that it is always sent #magmaCopy before it is likely to be
> used.

You could also call #beforeSerializingAny:do: to nil out (or do
whatever) to the PRContext.  For an example, see
MaObjectSerializer>>#setUpPreAndPostProcessing, where Magma's base code
that calls #flushCache on any Color before serializing it.

To use this, you would simply need to call this on your sessions
serializer:

  mySession serializer 
    beforeSerializingAny: #PRContext
    do: [ : aPRContext | aPRContext initializeProperties ]

If you need to do something after materializing, add the following:

; afterMaterializaingAny: #PRContext
  do: [ : aPRContext | "reset the properties" ]

If you can't restore the properties from the image then you need to use
the storage-object to capture whatever you need to restore it.

Finally, for anny of this to work, you *must* implement:

  maWantsPreSerialization
	^ true

and 

  PRContext>>#maWantsPostMaterialization
	^ true

 - Chris
> This doesnt seem like the most efficient way of doing things. I
> thought
> of using preSerialisation, but looking at the code it appears that
> the
> result of preserializing has to be the same object, it cant be a
> copy,
> otherwise it gets treated as a StorageObject.
> 
> At present it looks as though I can combine, copying my dangerous
> object, so as not to disturb the users of the dangerous properties,
> and
> using preserialisation to zap the properties before storing.
> 
> any advice would be appreciated.
> 
> Keith
> 
> 		
> ___________________________________________________________
> To help you stay safe and secure online, we've developed the all new
> Yahoo! Security Centre. http://uk.security.yahoo.com
> _______________________________________________
> Magma mailing list
> Magma at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/magma
> 
> 
> 



More information about the Magma mailing list