I have a ModuleLoader : NinjectModule which is where I bind everything.
Firstly I use
Bind<Form>().To<Main>();
to Bind a System.Windows.Forms.Form to my Main form.
Is this correct?
Secondly in the Program.cs I use this:
_mainKernel = new StandardKernel(new ModuleLoader());
var form = _mainKernel.Get<Main>();
Where _mainKernel is a ninject standard kernel.
Then I use Application.Run(form)
Is this correct?
I’m unsure as to what to bind together when it comes to Windows.Forms.
Thanks for any help.
You shouldn’t really be binding to
System.Windows.Forms.Form. Ninject is primarily meant for binding interfaces to concrete types so that you can pass around dependencies as interfaces and switch out the concrete implementation at runtime/during tests.If you just want to use Ninject to create your Form in this way though, you’d simply use
Bind<MyForm>().ToSelf()then dokernel.Get<MyForm>(). If you are requesting the concrete type directly though and it doesn’t take any dependencies, there’s not much point in using Ninject to initialise it.In your situation, if your form implements an interface then you would do:
Bind<IMainForm>().To<MainForm>()and request the interface type from Ninject. Usually your interface shouldn’t be bound to the concept of a “form” though, it should be agnostic of the implementation (so later you could produce a CLI and website version and simply swap the Ninject bindings).You could use the Model-View-Presenter design pattern (or a variant) to achieve this like: