title: Ninject for winforms - does my architecture make this useless?
body: <p>I'm trying out Ninject with a winforms app (basically a sketch, I'm using it sort of like a kata, but nothing so rigorous or specific) in .net 4.</p>
  
  <p>To create the main form, I'm doing something like:</p>
  
  <pre><code>static class Program
  {
      /// &lt;summary&gt;
      /// The main entry point for the application.
      /// &lt;/summary&gt;
      [STAThread]
      static void Main()
      {
          [...]
          IKernel kernel = BuildKernel();
          Application.Run(kernel.Get&lt;frmMain&gt;());
      }
  
      static IKernel BuildKernel()
      {
          var modules = new INinjectModule[] 
          { 
              [..modules]
          };
  
          return new StandardKernel(modules);
      }
  }
  </code></pre>
  
  <p>Fine. This creates a main form and displays it nicely, passing the appropriate interface implementations to the injected constructor.</p>
  
  <p>Now what? My application is an MDI and will have several child windows for manipulating the application model. I don't have a reference to the kernel anymore, so how am I supposed to <code>Get()</code> these forms? The obvious answer is 'pass the kernel to the form' I suppose, but that's a horribly messy strategy and I'm sure that doesn't fit into the philosophy of DI.</p>
  
  <p>I will point out here that the documentation for Ninject 2 <em>sucks</em>. Everything I can find repeats the basic examples, without really explaining how DI using Ninject makes anything easier. The standard of example given isn't complicated enough to make the trouble of coding and creating modules and bindings worthwhile.</p>
  
  <p>edit #1:</p>
  
  <p>Having studied the links kindly provided by <strong>Sam Holder</strong>, I'm trying out the 'composition root' approach. My architecture now forces all the Forms it uses to derive from a <code>CompositedForm</code> with constructor semantics thus:</p>
  
  <pre><code>    [Inject]
      public CompositingForm(ICompositionRoot CompositionRoot)
      {
          InitializeComponent();
          this.CompositionRoot = CompositionRoot;
      }
      public readonly ICompositionRoot CompositionRoot;
  
      public CompositingForm() : this(new DummyCompositionRoot()) { }
  </code></pre>
  
  <p>The second constructor is for the benefit of the Forms Designer, which is stupid and can't understand the form markup unless you provide an empty constructor. Now, every form created using <code>IKernel.Get&lt;AForm&gt;()</code> will (should) have a composition root injected into it. </p>
  
  <p>So, as I am a slow learner - now the question is really 'What should go in this composition root'?</p>