title: How to serialize objects created by factories
body: <p>I'm working on a project that uses dependency injection via Ninject. So far, it is working out very well, and I'm liking DI a lot, but now I have decided I need to serialize some objects, and I'm finding it difficult to do that following DI patterns.</p>
  
  <p>Say I have a class called Foo, which has a list of Bars, and makes them through a factory, like this:</p>
  
  <pre><code>public class Foo
  {
      private List&lt;Bar&gt; _bars;
      private BarFactory _barFactory;
  
      ...
  
      public void MakeBar()
      {
           _bars.Add(_barFactory.MakeBar());
      }
  }
  </code></pre>
  
  <p>Here's Bar, which gets made when <code>_barFactory.MakeBar()</code> gets called. I want Bar to be serializable:</p>
  
  <pre><code>public class Bar : ISerializable
  {
      private List&lt;IPickle&gt; _pickles;
      private PickleFactory _pickleFactory;
  
      public Bar(PickleFactory factory)
      {
           _pickleFactory = factory;
      }
  
      public void MakePickle(int x)
      {
           _pickles.Add(_pickleFactory.MakePickle(x));
      }
  
      public void GetObjectData(SerializationInfo info, StreamingContext context)
      {
          //serialize member variables here
      }
  
      //Constructor called during deserialization
      private Bar(SerializationInfo info, StreamingContext context)
      {
          //fill in member variables with data from SerializationInfo
      }
  }
  </code></pre>
  
  <p>Notice that Bar has its own factory and a collection of pickles. Here's the problem: when the deserialization constructor of Bar gets called, I don't have any way to get it another PickleFactory. The original PickleFactory was given to Bar by the BarFactory, but the deserialization constructor wasn't called by BarFactory.</p>
  
  <p>My current plan to solve this problem would be to extract all the serializable members of Bar into its own class called BarDataObject. Then I would make the BarDataObject serializable, but not Bar itself. I would add a function to BarFactory which accepts a BarDataObject as a parameter and builds a Bar for you filled in with all the information from the BarDataObject.</p>
  
  <p>However, suppose Pickle <em>also</em> has service classes which it got from the factory that made it, which can't be serialized either. So I would have to extract a DataObject from Pickle as well, and my BarDataObject would have to hold onto a PickleDataObject. And suppose Pickle had a member variable with a mix of data and services too? I would have to make and maintain a DataObject for that as well. This seems like a real bummer, especially considering my project has many other things which I need to serialize, and they'll probably face the same problem.</p>
  
  <p>So is there a better solution? Am I doing something wrong, DI-wise? I've just started working with DI and Ninject, but I can't seem to find anyone who's come up with a good way to serialize objects which got injected with service classes.</p>