Design pattern for converting objects to xml

We have a set of different POCO objects that represent the domain model for the application. Now we need to present an xml view that compiles data from different objects that will be consumed by some other application. We have an xml link on how the final presentation should look.

Based on the foregoing, I have two questions that primarily relate to best practices and optimization:

  • Given the target structure of xml, what is the best and recommended way to create an xml file based on data from different objects? Should I try to use the xsl transform or create a target class based on xsd and use custom translations etc.
  • Since this is part of the POC exercise, the XML transformation will finally be replaced by persistence of data in the database and / or conversion to JSON objects. I was wondering if I can use some design pattern to abstract away from the target implementation so that the primary code does not get hit when the xml generation procedure is replaced with a DAL call or JSON translation code. Any ideas?
+4
source share
2 answers

Create the target model (POCO-ish) based on the target xsd using the MS xsd tool and perform the conversion in the code through which it hides the translation implementation.

To simplify the conversion, you can use, for example, Automapper on github .

From the target model it would be easy to create XML, JSON or save data using, for example, the Entity Framework.

An example of JSON gene serialization:

public static string GetString<T>(T value) { using (var ms = new MemoryStream()) { var ser = new DataContractJsonSerializer(typeof(T)); ser.WriteObject(ms, value); byte[] json = ms.ToArray(); ms.Close(); return Encoding.UTF8.GetString(json, 0, json.Length); } } 

I have tried the XSLT route in the past, and although it is powerful, it tends to develop rapidly. With the target model above, you can debug your conversions, if necessary, and also offers you a number of opportunities to move forward.

Re: AutoMapper , in this case, when the target structure is complex and possibly different from the source, it is difficult to automatically display AutoMapper, I usually create Orchestrator / SuperMapper, which is responsible for the general structure / display, which uses AutoMapper internally.

Tip. If the target object requires values ​​from several, you can set it as a sequence of mappings to the same object by looking at this question / answer .

It is difficult to give a good general answer without special structures in mind.

Re: Facade Wikipedia has a good definition of this template β†’ Facade on Wikipedia But briefly define the simplest possible interface for your Orchestrator / SupperMapper so that you separate the internal work of translating the structure from the rest of your application. This way you can easily exchange it for something else when your needs change. Thus, the rest of the application does not need to know about AutoMapper or the target model. All he knows is to include the original model and expect, for example, to return Json.

+1
source

The design sample you are looking for is a data transfer object (DTO) . These are simple classes that have no behavior. You will then create an assembler to convert from your application domain model to DTO.

DTO can be easily created using xsd.exe , assuming you are using C # or VB.Net. I am sure you can find the equivalent for other languages.

After you set this setting, what you set inside the assembler for conversion is a little less critical, because it is beautifully encapsulated. It can be easily changed in the future if you need it. However, as Tommy says, I would advise against using XSLT. XSLT only allows you to change the shape of an XML document, it does not just allow things like value substitution (renaming names for identifiers) or calculations (sum of order lines to give a total number).

For more information see

http://martinfowler.com/eaaCatalog/dataTransferObject.html
http://msdn.microsoft.com/en-us/library/ms978717.aspx
http://en.wikipedia.org/wiki/Data_transfer_object

0
source

All Articles