Integrating Serialization and Deserialization in a Builder Design Pattern Using C #

I am implementing a Builder Pattern to create a set of objects. Then these objects must be serialized into XML and deserialized from XML.

I know how to serialize and deserialize, but I'm not sure how to integrate it into a design template.

For example, suppose my code uses the builder to create foo and bar products. My first thought is to put serialization on everyone, because every product knows what to serialize.

My next thought was to put deserialization in Director or ConcreteBuilder.

What I don't like about this is that the serialization and deserialization functions will be in different places - one in the file for declaring foo and bar objects, and the other in the files for something else. I am worried that they may stop synchronizing with each other when I work on product classes.

My last thought was that the director or ConcreteBuilder were serializing and deserializing. What I don't like about this is that then the products must know which builder was used or know who the Director is.

To clarify - there are two situations when a product can be created:

  • User clicks a button in the user interface
  • User uploads XML project
+6
source share
2 answers

My current solution is for the Product to serialize and ConcreteBuilder to deserialize and then place Product and ConcreteBuilder declarations in the same source file.

Although the task is divided into two classes, it is at least stored together in one file.

Any best solutions are appreciated.

0
source

Can you not just a static serialize / deserialize class and create a generic method that can accept any type of object? Isn't a template easy to create objects? Can you serialize as you want?

Sort of:

public static string Serialize<T>(T data) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); StringWriter sw = new StringWriter(); xmlSerializer.Serialize(sw, data); return sw.ToString(); } 
+1
source

Source: https://habr.com/ru/post/888553/


All Articles