title: Ninject dynamically bind to implementation
body: <p>There are several questions on Stack Overflow that are similar but not exactly what I'm looking for. I would like to do Ninject binding based on a runtime condition, that isn't pre-known on startup. The other questions on Stack Overflow for dynamic binding revolve around binding based on a config file or some such - I need to it to happen conditionally based on a database value while processing the data for a particular entity. E.g.,</p>
  <pre><code>public class Partner
  {
      public int PartnerID { get; set; }
      public string ExportImplementationAssembly { get; set; }
  }
  
  public interface IExport
  {
      void ExportData(DataTable data);
  }
  </code></pre>
  <p>Elsewhere, I have 2 dlls that implement IExport</p>
  <pre><code>public PartnerAExport : IExport
  {
      private readonly _db;
      public PartnerAExport(PAEntities db)
      {
          _db = db;
      }
      public void ExportData(DataTable data)
      {
          // export parter A's data...
      }
  }
  </code></pre>
  <p>Then for partner B;</p>
  <pre><code>public PartnerBExport : IExport
  {
      private readonly _db;
      public PartnerBExport(PAEntities db)
      {
          _db = db;
      }
      public void ExportData(DataTable data)
      {
          // export parter B's data...
      }
  }
  </code></pre>
  <p>Current Ninject binding is;</p>
  <pre><code>public class NinjectWebBindingsModule : NinjectModule
  {
      public override void Load()
      {
          Bind&lt;PADBEntities&gt;().ToSelf();
          Kernel.Bind(s =&gt; s.FromAssembliesMatching(&quot;PartnerAdapter.*.dll&quot;)
                            .SelectAllClasses()
                            .BindDefaultInterfaces()
                     );
      }
  }
  </code></pre>
  <p>So how do I set up the bindings such that I can do;</p>
  <pre><code>foreach (Partner partner in _db.Partners)
  {
      // pseudocode...
      IExport exportModule = ninject.Resolve&lt;IExport&gt;(partner.ExportImplementationAssembly);
      exportModule.ExportData(_db.GetPartnerData(partner.PartnerID));
  }
  </code></pre>
  <p>Is this possible? It seems like it should be but I can't quite figure how to go about it. The existing binding configuration above works fine for static bindings but I need something I can resolve at runtime. Is the above possible or am I just going to have to bypass Ninject and load the plugins using old-school reflection? If so, how can I use that method to resolve any constructor arguments via Ninject as with the statically bound objects?</p>
  <p><strong>UPDATE:</strong> I've updated my code with <code>BatteryBackupUnit</code>'s solution such that I now have the following;</p>
  <pre><code>Bind&lt;PADBEntities&gt;().ToSelf().InRequestScope();
  Kernel.Bind(s =&gt; s.FromAssembliesMatching(&quot;PartnerAdapter.*.dll&quot;)
                      .SelectAllClasses()
                      .BindDefaultInterfaces()
                      .Configure(c =&gt; c.InRequestScope())
              );
  
  Kernel.Bind(s =&gt; s.FromAssembliesMatching(&quot;PartnerAdapter.Modules.*.dll&quot;)
                      .SelectAllClasses()
                      .InheritedFrom&lt;IExportService&gt;()
                      .BindSelection((type, baseTypes) =&gt; new[] { typeof(IExportService) })
              );
  Kernel.Bind&lt;IExportServiceDictionary&gt;().To&lt;ExportServiceDictionary&gt;().InSingletonScope();
  ExportServiceDictionary dictionary = KernelInstance.Get&lt;ExportServiceDictionary&gt;();
  </code></pre>
  <p>Instantiating the export implementations within 2 test modules works and instantiates the <code>PADBEntites</code> context just fine. However, all other bindings in my services layer now no longer work for the rest of the system. Likewise, I cannot bind the export layer if I change <code>PADBEntities</code> variable/ctor argument to an ISomeEntityService component. It seems I'm missing one last step in configuring the bindings to get this work. Any thoughts?</p>
  <p>Error: &quot;Error activating ISomeEntityService. No matching bindings are available and the type is not self-bindable&quot;</p>
  <p><strong>Update 2</strong>: Eventually got this working with a bit of trial and error using <code>BatteryBackupUnit</code>'s solution though I'm not too happy with the hoops to jump thought. Any other more concise solution is welcome.</p>
  <p>I changed the original convention binding of;</p>
  <pre><code>        Kernel.Bind(s =&gt; s.FromAssembliesMatching(&quot;PartnerAdapter.*.dll&quot;)
                            .SelectAllClasses()
                            .BindDefaultInterfaces()
                     );
  </code></pre>
  <p>to the much more verbose and explicit;</p>
  <pre><code>Bind&lt;IActionService&gt;().To&lt;ActionService&gt;().InRequestScope();
  Bind&lt;IAuditedActionService&gt;().To&lt;AuditedActionService&gt;().InRequestScope();
  Bind&lt;ICallService&gt;().To&lt;CallService&gt;().InRequestScope();
  Bind&lt;ICompanyService&gt;().To&lt;CompanyService&gt;().InRequestScope();
  //...and so on for 30+ lines
  </code></pre>
  <p>Not my favorite solution but it works with explicit and convention based binding but not with two conventions. Can anyone see where I'm going wrong with the binding?</p>
  <p><strong>Update 3</strong>: Disregard the issue with the bindings in Update 2. It appears that I've found a bug in Ninject relating to having multiple binding modules in a referenced library. A change in module A, even though never hit via breakpoint will break a project explicitly using a different module B. Go figure.</p>