title: Simple Injector: How can I skip verification of an object in the container
body: <p>I'm using Simple Injector to inject dependencies into my objects via Constructor Injection.</p>
  
  <p>For a particular set of objects (all derived from a common abstract base class) I inject a factory instead of the concrete object so that I can determine at run-time which derived instance should be injected. The factory is registered with the container as a singleton, as are the derived instances.</p>
  
  <p>After registering all of my objects with the DI Container I call the Container.Verify() method to verify my configuration. The problem is, the implementation of this method creates an instance of every type registered with the container. These derived instances are expensive to create and their creation has side-effects (they come from legacy code that is being updated to use DI). Long-term I'll get rid of the side-effects but short term it's not feasable.</p>
  
  <p><strong>How can I tell the container not to verify these derived instances?</strong></p>
  
  <p>I want to keep the Verify() call (at the very least for debug builds) but can't accept these instances being instantiated by the Verify() call. Furthermore, they need to be registered with a Singleton Lifestyle so I can't just not register them with the container.</p>
  
  <p>One solution I came up with is to not register the derived objects with the container (thereby making them non-verifiable), and then have the (Singleton Lifestyle) Factory implementation cache the first instance it creates. It works, but it's dirty, and if someone explicitly requests an instance of the derived type somewhere else (unlikely) a new instance would be created (because the default Lifestyle is Transient).</p>
  
  <p>Here's an example of the class structure I have:</p>
  
  <pre><code>public class AbstractBase { }
  public class Derived1 : AbstractBase { }
  public class Derived2 : AbstractBase { }
  public class Derived3 : AbstractBase { }
  
  public class MyFactory
  {
      private readonly Func&lt;Derived1&gt; _factory1;
      private readonly Func&lt;Derived2&gt; _factory2;
      private readonly Func&lt;Derived3&gt; _factory3;
  
      public MyFactory(Func&lt;Derived1&gt; factory1, Func&lt;Derived2&gt; factory2, Func&lt;Derived3&gt; factory3)
      {
          _factory1 = factory1;
          _factory2 = factory2;
          _factory3 = factory3;
      }
  
      public AbstractBase Create()
      {
          if (AppSettings.ProductType == ProductType.Type1)
              return _factory1();
  
          if (AppSettings.ProductType == ProductType.Type2)
              return _factory2();
  
          if (AppSettings.ProductType == ProductType.Type3)
              return _factory3();
  
          throw new NotSupportedException();
      }
  }
  </code></pre>
  
  <p>And the registrations with the DI Container:</p>
  
  <pre><code>Container container = new Container();
  container.RegisterSingle&lt;Derived1&gt;();
  container.RegisterSingle&lt;Derived2&gt;();
  container.RegisterSingle&lt;Derived3&gt;();
  container.RegisterSingle&lt;Func&lt;Derived1&gt;&gt;(() =&gt; container.GetInstance&lt;Derived1&gt;());
  container.RegisterSingle&lt;Func&lt;Derived2&gt;&gt;(() =&gt; container.GetInstance&lt;Derived2&gt;());
  container.RegisterSingle&lt;Func&lt;Derived3&gt;&gt;(() =&gt; container.GetInstance&lt;Derived3&gt;());
  container.RegisterSingle&lt;MyFactory&gt;();
  
  container.Verify(); // &lt;-- will instantiate new instances of Derived1, Derived2, and Derived3. Not desired.
  </code></pre>