Serialize a custom property that is an entity

Posts   
 
    
ww
User
Posts: 83
Joined: 01-Oct-2004
# Posted on: 19-Apr-2012 19:33:17   

Using version 3.5, adapter.

Some of my entities need to hold an instance of another entity, but this relationship is not reflected in the database model since it's a class-level dependency rather than an instance-level dependency.

For example, all instances of EntityA will be holding a reference to an instance of EntityX. All instances of EntityB will be holding a reference to a different instance of EntityX.

EntityX itself has relations to other entities, including possibly other EntityAs and EntityBs.

The EntityAs, EntityBs, etc. get serialized for WCF. On the client side, the client will sometimes need the EntityX that is associated with a particular entity, so I'd like to serialize it along with the EntityA to avoid making a second server call to fetch it later.

I can't, as far as I've been able to figure out, use PerformCustomXmlSerialization, because of infinite recursion problems--working from within PerformCustomXmlSerialization there's no way to track which entities have already been serialized.

I thought about using a model-only relationship so the llblgen framework can handle the serialization fully, but that would require a model-only foreign key field, which isn't supported.

Can anyone think of a way to do this? Or should I just give up make a second server call when I need the EntityX?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Apr-2012 23:03:29   

ww wrote:

I can't, as far as I've been able to figure out, use PerformCustomXmlSerialization, because of infinite recursion problems--working from within PerformCustomXmlSerialization there's no way to track which entities have already been serialized.

PerformCustomXmlSerialization is the way to go. What are the problems you are having there? You can serialize the full graph recursively using a binaryFormatter. For instance:

MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
bf.Serialize(ms, o);
ms.Seek(0, SeekOrigin.Begin);
            
var toSerialize = ms.ToString();
David Elizondo | LLBLGen Support Team
ww
User
Posts: 83
Joined: 01-Oct-2004
# Posted on: 20-Apr-2012 15:46:59   

My problem is that the graph of the EntityX being serialized may contain the EntityA that is doing the serializing. I got a stack overflow because of recursion on this. I didn't try the binary formatter, though. I'll take another look.