title: Use MEF to compose parts but postpone the creation of the parts
body: <p>As explained in <a href="https://stackoverflow.com/questions/11951501/which-parts-of-mef-should-be-customized-for-dynamic-loading-of-plug-ins-in-a-dis">these</a> <a href="https://stackoverflow.com/questions/12328243/how-to-persist-mef-import-and-export-information-to-disk">questions</a> I'm trying to build an application that consists of a host and multiple task processing clients. With some help I have figured out how to discover and serialize part definitions so that I could store those definitions without having to have the actual runtime type loaded.</p>
  
  <p>The next step I want to achieve (or next two steps really) is that I want to split the composition of parts from the actual creation and connection of the objects (represented by those parts). So if I have a set of parts then I would like to be able to do the following thing (in pseudo-code):</p>
  
  <pre><code>public sealed class Host
  {
      public CreationScript Compose()
      {
          CreationScript result;
          var container = new DelayLoadCompositionContainer(
              s =&gt; result = s);
          container.Compose();
          return script;
      }
  
      public static void Main()
      {
          var script = Compose();
  
          // Send the script to the client application
          SendToClient(script);
      }
  }
  
  // Lives inside other application
  public sealed class Client
  {
      public void Load(CreationScript script)
      {
          var container = new ScriptLoader(script);
          container.Load();
      }
  
      public static void Main(string scriptText)
      {
          var script = new CreationScript(scriptText);
          Load(script);
      }
  }
  </code></pre>
  
  <p>So that way I can compose the parts in the host application, but actually load the code and execute it in the client application. The goal is to put all the smarts of deciding what to load in one location (the host) while the actual work can be done anywhere (by the clients).</p>
  
  <p>Essentially what I'm looking for is some way of getting the ComposablePart graph that MEF implicitly creates.</p>
  
  <p>Now my question is if there are any bits in MEF that would allow me to implement this kind of behaviour? I suspect that the <a href="http://mefcontrib.codeplex.com/wikipage?title=Provider%20model" rel="nofollow noreferrer">provider model</a> may help me with this but that is a rather large and complex part of MEF so any guidelines would be helpful.</p>