title: Serializing Data Transfer Objects in .NET
body: <p>I have a set of data transfer objects (e.g. a lot of reqest, response message classes, like MainRequest, MainResponse, ShutDownRequest, ShutDownResponse) <em>Where new classes keep coming as the project evolves</em>. These classes must be (de)serialized from and to <strong>various XML formats with different public XSDs</strong>. <em>New XML formats come as the project evolves too.</em></p>
  
  <p>My question here is how I would design my classes and interfaces around <em>these two requirement</em>, especially where I should put the actual (de)serilization logic. Should I write a static service that can take the various DTO instances an knows how to serialize each of them? When new classes come I have to touch each FormatXSeriaizer and add new overrides. As new formats come I just have to write new FormatXSerializer classes.</p>
  
  <pre><code>FormatASerializer.Serialize(XmlWriter writer, MainResponse r);
  FormatASerializer.Serialize(XmlWriter writer, ShutDownResponse r);
  FormatBSerializer.Serialize(XmlWriter writer, MainResponse r);
  FormatBSerializer.Serialize(XmlWriter writer, ShutDownResponse r);
  </code></pre>
  
  <p>or should the DTOs themself know how to do it. So I have it all in one place - for each DTO class. As new DTO classes come, they just have to implement the serialization for the various formats. As new formats come, I have to touch each DTO class.</p>
  
  <pre><code>myMainRequestInstace.Serialize(FormatTypeEnum type, XmlWriter writer);
  </code></pre>
  
  <p>Or is there a complete different approach? Should I introduce a common inteface for serialization and have some inversion of control, so I could load new format serializers at runtime? </p>
  
  <p>What design pattern can guide me here?</p>
  
  <p>What open source code in the .NET world could I study to see different approaches on this subject?</p>
  
  <p><strong>EDIT:
  I know about the general serialization techniques existing in the framework. My question is more geared towards class design that respects the two requirements: multiple xml formats and multiple DTOs (message types) that keep coming as the project evolves.</strong></p>