title: Simple Injector Prevent Locked Container
body: <p>I need to know if there is a way to prevent the container from locking after calling <code>GetInstance</code>? </p>
  
  <p>I have an implementation <code>INamedInstanceFactory&lt;T&gt;</code> where T : Xamarin.Forms.Page, and I need to have an association between a string and a specific concrete type. Unfortunately in order to make it work I have to resolve <code>INamedInstanceFactory&lt;Page&gt;</code> before all registrations are complete. </p>
  
  <p>How can I prevent the container from being locked?</p>
  
  <p>Edit: 
  We start with an extension like:</p>
  
  <pre><code>public static void RegisterPage&lt;T&gt;(this Container container, string name)
      where T : Page
  {
     // Do Platform stuff
     var namedInstance = continer.GetInstance&lt;INamedInstanceFactory&lt;Page&gt;&gt;();
     var namedInstance.Register( typeof( T ), name, Lifestyle.Transient );
  }
  </code></pre>
  
  <p>Note: this sample uses a transient lifestyle but other services may require a singleton.</p>
  
  <pre><code>public class NamedInstanceFactory&lt;T&gt; : Dictionary&lt;string, Type&gt;, INamedInstanceFactory&lt;T&gt;
  {
      Container _container { get; }
      public NamedInstanceFactory(Container container)
      {
          _container = container;
      }
  
      public virtual T CreateNew(string name)
      {
          if( ContainsKey(name))
              return (T)_container.GetInstance(this[name]);
          return default(T);
      }
  
      public virtual void RegisterType(Type type, string name, Lifestyle lifestyle = null)
      {
          if (type == null) throw new Exception(
              $"The NamedInstanceFactory&lt;{typeof(T).Name}&gt; cannot register null types");
  
          if (string.IsNullOrWhiteSpace(name))
          {
              name = type.Name;
          }
  
          if (lifestyle == null)
          {
              lifestyle = Lifestyle.Transient;
          }
  
          this[name] = type;
          _container.Register(type, type, lifestyle);
      }
  }
  </code></pre>
  
  <p>And of course INamedInstanceFactory&lt;> is registered as:</p>
  
  <pre><code>Container.Register(typeof(INamedInstanceFactory&lt;&gt;), typeof(NamedInstanceFactory&lt;&gt; ), 
      Lifestyle.Singleton);
  </code></pre>
  
  <p>Keeping with the page example. We might have a NavigationStack expressed as <code>MainPage/PageA/PageB/PageC</code> and we need to be able to parse that and resolve the actual page type for the key "MainPage" or "PageA" etc so to create the page object we would then have a function like:</p>
  
  <pre><code>public Page CreatePage(string name)
  {
      return namedInstanceFactory.CreateNew(name);
  }
  </code></pre>