Question

I am generating classes from XSD and need to populate the classes to serialize to xml.

I have different classes, containing all the info that goes into the classes generated.

The problem is that the generated classes come in versions, and properties in those classes are other classes in the same version.

class LocalData
{
   public MyClass property { get; set; }
}

class XmlVersion1
{
   public MyClassV1 property { get; set; }
}

class XmlVersion2
{
   public MyClassV2 property { get; set; }
   public MyClassXV2 newProperty { get; set; }
}

The data in MyClassV1 and V2 are basically the same, so the same code can be used.

I wanted to make a factory that just took the LocalData class and any of the versioned classes and populated the data in the versioned class, but I run into a problem when I want to do property = new MyClassVx, because the factory does not know which version it's supposed to create.

I could do

if (parameter is MyClassV1)
   paramter.MyClassV1 = new MyClassV1

and so on, but that is a LOT of code.

This is for generating xml messages that are specified by an external company, and they come in different versions, and we have to be able to serialize and deserialize the content into our internal system.

Was it helpful?

Solution

We have not found a solution to this specific issue and chose to use AutoMapper which seems to solve our problem in a different way.

We made a tool that takes the generated classes and creates the mapping classes needed for AutoMapper through assembly. If you have large generated classes you could do this as well. We can now create thousands of lines of code needed to map classes. It solves an issue we had when mapping types of 'object' to specific classes. I don't know if it's helpful but there it is.

OTHER TIPS

Idea is very simple. Factory will not care about version. But newer clients will always support old versions' features. If version 0.5 has a method to receive orders list (for example this is a shopping app) version 0.6 also should have same method. We have same structure and we are doing this way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top