XML serialisation of nested entities

Posts   
 
    
Posts: 254
Joined: 16-Nov-2006
# Posted on: 06-May-2008 00:35:35   

Using latest runtime library and self servicing.

I took the code in the help documentation

StreamWriter writer = new StreamWriter("customer.XML");
string customerXml = String.Empty;
customer.WriteXml(out customerXml);
writer.Write(customerXml);
writer.Close();

And tried it with an entity which has nested entities e.g. the customer contains the address within an address entity but also has name properties which are primitive types. All the primitive types and values of the top level entitiy i.e. customer here serialise fine however nested entities dont.

They only seem to serialise if you retrieve the data i.e. you add some code such as

AddressEntity address = customer.Address;

1) Is it possible to use WriteXml methods or any serialisation to serialise the whole object and all related entities without having to retrieve all the data through code e.g. just by finding a customer by the identifier? The XmlFormatAspect enumeration values don't seem to have any options related to this.

2) If 1 is not possible, is there any quick way or could a generic function be written to retrieve all related entities for an LLBLGEN EntityBase derived object. At least if 1 isn't possible, this generic method could then be called followed by WriteXml to serialise all data.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-May-2008 03:05:02   

Hi Matt. SelfServicing's WriteXml() writes only already fetched related entities. Otherwise would be tricky (how many related entities, how many levels, etc.). So option 1 isn't possible.

Option 2 would be possible using PrefetchPaths. Read LLBLGenPro Help - Using the generated code - SelfServicing - Prefetch paths. As soon as you have fetched using GetMulti with prefetchpath you can Serialize/Deserialize the whole graph.

David Elizondo | LLBLGen Support Team
Posts: 254
Joined: 16-Nov-2006
# Posted on: 06-May-2008 11:49:47   

Otherwise would be tricky (how many related entities, how many levels, etc.). So option 1 isn't possible.

I could understand it being tricky to implement on partial levels of retrieval however it would be useful to have an option to serialise all levels.

Regards option 2 if you weren't overly worried about optimising performance, isn't there a generic method that could be written to simply retrieve all related entities from a given entity. I presume this would be possible, perhaps theres an internal framework method in the runtime which could do this which could be exposed as public in some way.

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 06-May-2008 15:58:53   

You can easily end up by fetching the entire database.

What's better to do is to use PrefetchPaths, to define what levels you want to fetch along with the main entity.

hint: the prefetchPath can be passed in the CTor of an entity to use it in a Single entity fetches.

Posts: 254
Joined: 16-Nov-2006
# Posted on: 06-May-2008 16:30:50   

You can easily end up by fetching the entire database.

How could you end up fetching the entire database? If you have an entity e.g. Person with sub entities Address, Contact e.t.c. why wouldn't this be possible?

In anycase if you did have circular links back to the main entity surely this could be detected in code e.g. a flag to determine whether the entity has been visited and fetched before. This seems generic and useful code to fetch all levels deep for a given entity.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 06-May-2008 16:53:29   

I don't see what the problem is?

You fetch the graph you want to serialize, using prefetch paths, then call WriteXml, which will serialize all entities in the graph. This will take care of cyclic references etc.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 254
Joined: 16-Nov-2006
# Posted on: 06-May-2008 17:25:02   

Sure you can do this however it would be nice and I don't think too difficult to create a generic method which works against any Entity and retrieves all references.

This would not only avoid me writing code to use the prefetch paths for each entity I wanted to do this, but also help others achieve the same thing in a reusable method.

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 06-May-2008 18:07:38   

Sure you can do this however it would be nice and I don't think too difficult to create a generic method which works against any Entity and retrieves all references.

You can always create this method yourself.

I'm closing this thread, please feel free to reopen it if you have further questions.