2

I am trying really hard to keep my base classes "clean" while using XML serialization and was wondering if there is any other option. Let me explain first what the situation is.

I have base classes that look like that:

public class Project
{
      public string Name { get; set; }

      public List<AbstractFlow> Flows { get; set; }         
}

public abstract class AbstractFlow
{
      public string Name { get; set; }

      [XmlArray("Steps")]
      [XmlArrayItem("Step", Type = typeof(AbstractSolutionSerializer<AbstractStep>))]
      public List<AbstractStep> Steps { get; set; }
}

public abstract class AbstractStep
{
      public string Name { get; set; }

      public int Length { get; set; }
}

These are inside a library project and the derived classes reside within another library. I use XML serialisation using a custom AbstractProjectSerializer which implements IXmlSerializable and I need to create an intermediate class:

[XmlRoot("ProjectSerializer")]
public sealed class ProjectSerializer
{     
       public ProjectSerializer(Project project)
       {
             [XmlElement("Name")]
             public string Name { get; set; }

             [XmlArray("Flows")]
             [XmlArrayItem("Flow", Type = typeof(AbstractProjectSerializer<AbstractFlow>))]
             public string List<AbstractFlow> { get; set; }
       }
}

I am happy with the intermediate class above however as you notice I needed to go in AbstractFlow and add XML serialization specific attributes. This for me "pollutes" the base classes and ties them with XML serialization. Is there any other way I can achieve what I want?

Dimitris
  • 2,030
  • 3
  • 27
  • 45
  • 2
    We actually make a distinction between DTO classes which are serializable, and other classes. So we put all the XML serialization only into the DTO classes, and transform to/from the other classes when needed (using Automapper if useful). Obviously this introduces a lot of new DTO classes, but it really helps with SRP. – Matthew Watson Jan 08 '15 at 12:32
  • Oh I so wanted to avoid DTO classes. The example above is very simple but I have quite complex classes. Hmmmm I used to have binary serialization and recently decided to move everything to XML. – Dimitris Jan 08 '15 at 12:34
  • 1
    We did without DTO classes for a long time, but eventually we realised that not using them was impeding code quality and robustness. Versioning of persisted objects in particular was much harder to do without using a separate DTO for serialization. – Matthew Watson Jan 08 '15 at 12:36
  • Damn it, I knew it would get down to using DTO classes but I wanted to avoid them. I don't get why its not straightforward to serialize in XML like you do in binary. Anyway thanks for the heads up. – Dimitris Jan 08 '15 at 12:38
  • Well you can serialise to XML in exactly the same way as you serialize to binary, you just use an XML writer rather than a binary writer - this is if you are using the old [Serialiazable] attribute. Want a code sample? – Matthew Watson Jan 08 '15 at 13:09
  • No I am all right thanks, I have the code to do this. – Dimitris Jan 08 '15 at 13:48

0 Answers0