Note that there are some explanatory texts on larger screens.

plurals
  1. POPrevent Ninject from calling Initialize multiple times when binding to several interfaces
    primarykey
    data
    text
    <p>We have a concrete singleton service which implements <code>Ninject.IInitializable</code> and 2 interfaces. Problem is that services Initialize-methdod is called 2 times, when only one is desired. We are using .NET 3.5 and Ninject 2.0.0.0.</p> <p>Is there a pattern in Ninject prevent this from happening. Neither of the interfaces implement <code>Ninject.IInitializable</code>. the service class is: </p> <pre><code>public class ConcreteService : IService1, IService2, Ninject.IInitializable { public void Initialize() { // This is called twice! } } </code></pre> <p>And module looks like this:</p> <pre><code>public class ServiceModule : NinjectModule { public override void Load() { this.Singleton&lt;Iservice1, Iservice2, ConcreteService&gt;(); } } </code></pre> <p>where Singleton is an extension method defined like this:</p> <pre><code> public static void Singleton&lt;K, T&gt;(this NinjectModule module) where T : K { module.Bind&lt;K&gt;().To&lt;T&gt;().InSingletonScope(); } public static void Singleton&lt;K, L, T&gt;(this NinjectModule module) where T : K, L { Singleton&lt;K, T&gt;(module); module.Bind&lt;L&gt;().ToMethod(n =&gt; n.Kernel.Get&lt;T&gt;()); } </code></pre> <p>Of course we could add bool initialized-member to ConcreteService and initialize only when it is false, but it seems quite a bit of a hack. And it would require repeating the same logic in every service that implements two or more interfaces.</p> <hr> <p>Thanks for all the answers! I learned something from all of them! (I am having a hard time to decide which one mark correct).</p> <p>We ended up creating IActivable interface and extending ninject kernel (it also removed nicely code level dependencies to ninject, allthough attributes still remain). </p>
    singulars
    1. This table or related slice is empty.
    plurals
    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