Design mode – object to XML conversion design mode

We have a set of different POCO entities that represent the domain model of the application. Now we need to come up with an xml representation to compile data from different entities, which will be used by some other applications . We have a reference xml on how the final representation should be.

Based on the above, I have two main questions for best practice and optimization:

> Given the target xml structure, what is the best and recommended way to create xml files based on data from different entities? I should try to use xsl transformation or generate target class based on xsd and use custom translation etc.
>Since this is part of the POC exercise, the XML transformation will eventually be replaced with data persistence in the database or/and converted to JSON Object. I want to know if I can use some design patterns to abstract the goal realization so that when the xml generation routine is called by DAL or replaced by JSON conversion code, the main code will not be affected. Any ideas?

Use the MS xsd tool to create a target model (POCO-ish) based on the target xsd, and hide the translation Transform the code of the realized appearance.

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

From the target model, you can easily use, for example, Entity Framework to generate XML ,JSON or persistent data.

Example of genric JSON serialization:

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

I have tried the XSLT route in the past. Although it is powerful, it has a tendency to be fast and complex. Using the target model above, you can debug the transformation as needed, and it also provides you with A series of opportunities to move forward.

Re: AutoMapper, in this case, when the target structure is complex and may be different from the source, it is difficult to let AutoMapper directly map everything, I usually create an Orchestrator / SuperMapper, responsible for the overall structure/mapping of AutoMapper internally.

Tips: If the target requires multiple values, you can set it as a sequence mapped to the same object, please refer to this question/answer.

p>

Without a specific structure, it is difficult to give a good general answer.

Re: Faca de Wikipedia has a good definition of this pattern >> Facade on Wikipedia But in short, define the simplest interface for Orchestrator/SupperMapper in order to separate the structure from the internal workings of the rest of the application. This way, when you When your needs change, you can easily replace it with something else. This way, the rest of your application does not need to know AutoMapper or the target model. All it knows is to put in the source model and expect Json to come back.

We have a set of different POCO entities, which represent the domain model of the application. Now we need to come up with an xml representation to compile data from different entities, which will be used by some other applications Program usage. We have a reference xml on how the final representation should be.

Based on the above, I have two main questions for best practice and optimization:

> Given the target xml structure, what is the best and recommended way to create xml files based on data from different entities? I should try to use xsl transform or generate target class based on xsd and use custom translation etc.
>Since this is part of the POC exercise, the XML conversion will eventually be replaced with data persistence in the database or/and converted to JSON Object. I want to know if I can use some design patterns to abstract the goal realization so that when the xml generation routine is called by DAL or replaced by JSON conversion code, the main code will not be affected. Any ideas?

Use the MS xsd tool to create a target model (POCO-ish) based on the target xsd, and perform code conversion by hiding the appearance of the translation.

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

From the target model, you can easily use, for example, Entity Framework to generate XML, JSON or persistent data.

< p>Example of genric JSON serialization:

public static string GetString(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);
}
}

Me I have tried the XSLT route in the past. Although it is powerful, it has a tendency to be fast and complex. Using the target model above, you can debug the transformation as needed, and it also provides you with a series of opportunities to move forward.

Re: AutoMapper. In this case, when the target structure is complex and may be different from the source, it is difficult to let AutoMapper directly map everything. I usually create an Orchestrator/SuperMapper, responsible for the internal use of AutoMapper’s overall structure/mapping.

Tip: If the target requires multiple values, you can set it as a sequence mapped to the same object, please refer to this question/answer.

Without a specific structure, it is difficult Give a good general answer.

Re: Facade Wikipedia has a good definition of this pattern >> Facade on Wikipedia But in short, Orchestr ator/SupperMapper defines the simplest interface in order to separate the structure from the internal workings of the rest of the application. This way, when your needs change, you can easily replace it with something else. In this way, your application’s The rest does not need to know AutoMapper or the target model. All it knows is to put in the source model and expect Json to come back.

Leave a Comment

Your email address will not be published.