title: Dependency Injection in WinForms using Ninject and Entity Framework
body: <p>I am currently working on a project where we are converting our old DataSet, Stored Procedure WinForm application to use Entity Framework so new websites can access the same object model and repositories.</p>
  
  <p>Anyway, I am trying to implement Dependency Injection into the Forms so that we can use mocked Repositories for unit testing. I am using Ninject for the the simple reason I have used it before in MVC websites, however trying to implement this in the WinForm application is proving to be problematic to say the least, hampered even more by the lack of information on DI in WinForms on the web.</p>
  
  <p>So far I have created the Ninject Factory and repositories, but I haven't had much luck injecting the repositories into forms.</p>
  
  <p>Therefore can anyone help me or make any suggestions?</p>
  
  <p>Below I have parts of my code that might help:</p>
  
  <p>Ninject Factory:</p>
  
  <pre><code>public class NinjectFactory : NinjectModule
  {
      private IKernel _ninjectKernel;
  
      public NinjectFactory()
      {
          _ninjectKernel = new StandardKernel();
      }
  
      public override void Load()
      {
          _ninjectKernel.Bind(typeof(IRepository&lt;&gt;)).To(typeof(GenericRepository&lt;&gt;));
          _ninjectKernel.Bind(typeof(IProductionRepository)).To(typeof(ProductionRepository));
      }
  }
  </code></pre>
  
  <p>Form with repositories:</p>
  
  <pre><code>Public Class TaskForm
      Inherits BaseBusinessDialogForm
  
      Private _repository As TaskRepository
      Private _task As Production.Task = Nothing
  
      Public Sub New(ByVal task As Production.Task)
  
          InitializeComponent()
  
          _repository = New TaskRepository(ConnectString)
  
          If task.TaskID = 0 Then
              _task = task
          Else
              _task = _repository.GetByID(task.TaskID)
          End If
  
          MyBase.BusinessObject = _task
          Me.TaskBindingSource.DataSource = MyBase.BusinessObject
  
      End Sub
  </code></pre>
  
  <p>Class that launches the MDI form which holds the above form:</p>
  
  <pre><code>Dim kernel As IKernel = New StandardKernel(New NinjectFactory())
  ''Dim kernel As IKernel = New StandardKernel(New NinjectFactoryTest())
  mfrmMDI = kernel.Get(Of Forms.MDI)()
  
  Application.DoEvents()
  mfrmMDI.ShowDialog()
  </code></pre>
  
  <p>I understand that my question is a bit vague, but I'm not sure where the problem lies or what I need to complete.</p>
  
  <p>Thanks very much</p>