Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would make the dependency be at the form-to-form level. You want to have something in between that. </p> <pre><code>namespace Example { public class SettingsRepository { public SettingsRepository() { } } public class SettingsForm { private SettingsRepository _settingsRepository; public SettingsForm( SettingsRepository settingsRepository ) { _settingsRepository = settingsRepository; } } public class MainForm { private SettingsRepository _settingsRepository; private Func&lt;SettingsForm&gt; _createSettingsForm; public MainForm( Func&lt;SettingsForm&gt; createSettingsForm, SettingsRepository settingsRepository ) { _createSettingsForm = createSettingsForm; _settingsRepository = settingsRepository; } } } </code></pre> <p>Then you inject a <code>Func&lt;SettingsForm&gt;</code> into your class to remove the direct usage of the container / Kernel from your code (if you're doing inline <code>Get</code> calls all over the place, you're doing Service Location, which is a different thing to DI entirely).</p> <pre><code> public class ExampleNinjectModule : NinjectModule { public override void Load() { Bind&lt;Func&lt;SettingsForm&gt;&gt;().ToMethod( context =&gt; () =&gt; context.Kernel.Get&lt;SettingsForm&gt;() ); } } </code></pre> <p>Another approach is to add a <code>Kernel</code> to your constructor args (Ninject automatically resolves it), but that quickly becomes a mess in general.</p> <p>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.</p> <p>Bottom line is you wont go far wrong if you:</p> <ol> <li>stick with Constructor Injection, and avoiding direct usage of Kernel or container attributes in your real code as much as possible</li> <li>Dont use a Global Kernel and/or Service Location</li> </ol> <p>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)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload