Create by id

10 views
Skip to first unread message

Yonas

unread,
Aug 15, 2010, 9:34:22 PM8/15/10
to FxWorks
Hi Alex

Love this library! I have question for you.

Is there a way to automatically retrieve an item from the object cache
by its id from another object?
So if I have some xml like this:

<JobApplication>
<id>1</id>
<jobId>2</jobId>
</JobApplication>

And I have an JobApplication object with a strictly typed Job
property.
When the xml gets deserialised I want the Application to have it's job
property set by retrieving it from the object cache (or creating a new
one if does not exist) by the jobId value in the xml

Is there an easy way to do this with tags without writing a custom
deserializer by implementing IXmlSerializable? The easiest way I've
found it to have a getter setter for another property jobID within
JobApplication which then creates/retreives the job manually.

A related question is what's the best way to have an object
(de)serialised through metadata tags but then do some extra
(de)serialising in code. Because if you implement IXmlSerializable you
have to do everything manually instead of just adding to what the the
tags already accomplish

Hope this all makes sense :)

Yonas

unread,
Aug 17, 2010, 6:20:39 AM8/17/10
to FxWorks
Ahh I can see this issue that you opened describes this feature as
well
http://code.google.com/p/flexxb/issues/detail?id=5

Yonas

unread,
Sep 9, 2010, 12:48:24 AM9/9/10
to FxWorks
Anyone haev any ideas?
At the moment I'm just doing this:

import au.com.massive.flexLib.managers.ObjectCache;

public class ApplicationVO
{
public var job:JobVO;

[XmlElement(ignoreOn='serialize')]
public function set jobId(value:String):void
{
job = ModelObjectCache.instance.getObject(value, JobVO) as JobVO;
}
}


This isn't a very clean way though as it ties the class to the FlexXB
api, and also isn't possible when you don't have access to the source
of the file, such as when it's already compiled in a swc

Alexutz

unread,
Sep 9, 2010, 3:21:53 AM9/9/10
to FxWorks
You can register listeners on the FlexXBEngine for the postDeserialize
event; FlexXB triggers some events to signal various steps in the
processing flow: preserialize, postserialize, predeserialize and
postdeserialize. In the handler, which gets called after each object
is deserialized you can then check if the object is of type
ApplicationVO and then set the job on it:

FlexXBEngine.instance.addEventListener(XmlEvent.POST_DESERIALIZE,
postDeserializehandler);
//or this if you do not use the easy access instance
//flexxbInstance.addEventListener(XmlEvent.POST_DESERIALIZE,
postDeserializehandler);

private function postDeserializehandler(event : XmlEvent) : void{
var result : Object = event.object;
if (result is ApplicationVO) {
ApplicationVO(result).job =
ModelObjectCache.instance.getObject(ApplicationVO(result).value,
JobVO) as JobVO;
}
}

Something like this should work. Check it though, as I did not write
it in a project but rather directly here. :D

Hope it helps,
Alex

Yonas Kolb

unread,
Aug 6, 2012, 10:16:04 AM8/6/12
to fle...@googlegroups.com
Hi Alex

I finally ended up implementing this myself. I'm taking advantage of the idRef and serializePartialElement annotation properties to control this behaviour. To turn this behaviour on these must both be set to true, as well as providing a cache provider.

Serialization stays the same, because when idRef is on it serializes how I want it by default.
On derialisation, there is extra behaviour to read in an id and return it from the cache (in a similar format to how idRef serializes the same element. The deserialiser only adds the id ref resolution path if serializePartialElement is false for backwards compatiability with old code that uses idRefs. I don't think anyone would otherwise have these two annotation properties both on at the same time.

If I have some time I can make a patch and you can see if you want to integrate it. I would have put this functionality into another annotation property but this was easier, plus the idRef property already has a good name for this and having another similar one would be confusing.
Reply all
Reply to author
Forward
0 new messages