I try to create aplication with Ninject. I have one MainForm and dialog form for settings. How should i implement this? Should MainForm have a Kernel instance and then create SettingsForm with Get<SettingsForm>() method? Or should MainForm constructor have parameter with SettingsForm instance?
I tried find some example of WinForm application with ninject, but i found only some ASP.NET applications which are useless in Visual C# 2008 express.
I would make the dependency be at the form-to-form level. You want to have something in between that.
Then you inject a
Func<SettingsForm>into your class to remove the direct usage of the container / Kernel from your code (if you’re doing inlineGetcalls all over the place, you’re doing Service Location, which is a different thing to DI entirely).Another approach is to add a
Kernelto your constructor args (Ninject automatically resolves it), but that quickly becomes a mess in general.I tried a quick search for samples, but sadly didnt find anything quickly in the WinForms space. I’d suggest perhaps looking for WPF examples instead.
Bottom line is you wont go far wrong if you:
Update Sep 12: These days one would definitely employ Ninject.Extensions.Factory to manage the factory (i.e., most of the code would above would be auto-genned behind the scenes)