Exception when using XmlSerialization with two versions of the same entity

Posts   
 
    
acl
User
Posts: 94
Joined: 28-Mar-2012
# Posted on: 08-Dec-2020 12:20:50   

Hi,

I'm using Xml.Serialization.XmlSerializer to serialize an entity graph (LLBLGen Pro runtime framework version 5.7.1). According to the documentation, the format used is the Compact25 format.

The use case for this is transferring an entity graph from one customer installation to another.

I just noticed that there is a problem when the entity types on both sides (serialize/deserialize) are different. In my case, the code that serializes the entities has one less property than the code that deserializes it. So basically, the customer who deserializes has an entity type that contains a property mapped to a table column "SomeNewField" that does not exist in the serialized entity description.

This leads to an exception when deserializing (There is an error in XML document (291, 8 ).) with the following InnerException:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

at System.Collections.BitArray.Get(Int32 index) at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2.Xml2Entity(XmlReader reader, Dictionary2 processedObjectIDs, List1 nodeEntityReferences) at SD.LLBLGen.Pro.ORMSupportClasses.EntityCore1.ReadXml(XmlReader reader, XmlFormatAspect format) at SD.LLBLGen.Pro.ORMSupportClasses.EntityCore1.ReadXmlExplicitImpl(XmlReader reader) at SD.LLBLGen.Pro.ORMSupportClasses.EntityCore`1.System.Xml.Serialization.IXmlSerializable.ReadXml(XmlReader reader) at System.Xml.Serialization.XmlSerializationReader.ReadSerializable(IXmlSerializable serializable, Boolean wrappedAny) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderList1.Read1_ArrayOfTImpExpEntity()

This happens on one of the many <_lps> nodes. If we remove them from the XML document, everything works well.

I really don't care about all the state that's saved there. What I'm interested in is just to get the values of the generated properties across. If there is a property missing, I just expect it to be set to its default value. If there is a property too much, I just expect it to be ignored. Kind of what the basic .NET XmlSerializer does when serializing classes.

Is there a way to prevent these <_lps> nodes from being generated? Or am I approaching the use case the wrong way?

Thanks,

andreas

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39861
Joined: 17-Aug-2003
# Posted on: 08-Dec-2020 14:57:32   

lps nodes are there to serialize the entity states across. They're meant to be as compact as necessary so there's little checking as it's expected the same types are present at both sides. If you have different types, it's not supported.

If you want to get data across, it might help to use dto's instead? There's no setting to omit the state nodes, as without them it might be easier to transport the data in other object types (as the concept of an entity isn't needed). Better is to keep the same types on both sides in these situations. I know this isn't always possible, but serializing data across services with different types is asking for trouble

Frans Bouma | Lead developer LLBLGen Pro
acl
User
Posts: 94
Joined: 28-Mar-2012
# Posted on: 08-Dec-2020 15:37:08   

Thanks Otis. What do you mean by "dto" ?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 08-Dec-2020 19:54:02   

DTO stands for Data Transfer Object, Sometimes called POCO classes as well (Plain Old CLR Object), though a POCO class might hold behavior, DTOs don't . Just a simple class/structure to hold the data.

LLBLGen Pro enables you to project fetches into DTOs instead of entities.

You can define the DTO yourself at code time, so you create those classes in code, and then use them to project data and transfer them over the wire. Typed Projection

Project DynamicLists into POCO

Also you can design these classes in the Designer and let the Designer generate the DTO classes for you. Please check Derived Models

acl
User
Posts: 94
Joined: 28-Mar-2012
# Posted on: 09-Dec-2020 09:56:05   

Thanks Walaa. I was hoping to avoid creating POCO classes just for this. But I'll look into designer generated DTOs. They seem to be what's needed here.