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.
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.
So far I have created the Ninject Factory and repositories, but I haven't had much luck injecting the repositories into forms.
Therefore can anyone help me or make any suggestions?
Below I have parts of my code that might help:
Ninject Factory:
public class NinjectFactory : NinjectModule
{
private IKernel _ninjectKernel;
public NinjectFactory()
{
_ninjectKernel = new StandardKernel();
}
public override void Load()
{
_ninjectKernel.Bind(typeof(IRepository<>)).To(typeof(GenericRepository<>));
_ninjectKernel.Bind(typeof(IProductionRepository)).To(typeof(ProductionRepository));
}
}
Form with repositories:
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
Class that launches the MDI form which holds the above form:
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()
I understand that my question is a bit vague, but I'm not sure where the problem lies or what I need to complete.
Thanks very much