title: Making Custom IoC - How to Implement DI That Has Scope?
body: <p>I'm writing an IoC container for my own learning/growth. Normally I would write something like the following:</p>
  
  <pre><code>using(DisposableObject dispObj = new DisposableObject())
  {
      UserRepository users = new UserRepository(dispObj);
  
      // Do stuff with user.
  }
  </code></pre>
  
  <p>Would turn to:</p>
  
  <pre><code>using(IDisposableObject dispObj = Container.Resolve&lt;IDisposableObject&gt;())
  {
      IUserRepository users = Container.Resolve&lt;IUserRepository&gt;();
  
      // Do stuff with user.
  }
  </code></pre>
  
  <p>How can I abstract the DisposableObject so that it is the only instanced used in the <code>using</code> scope when using an IoC? I'm trying to figure out how Autofac does it but I'm not totally sure.</p>
  
  <p><strong>EDIT:</strong> When an object is instantiated with the <code>using</code> scope, all calls to resolve that type (in this case <code>IDisposableObject</code>) should return the scoped variable, and not a new instance. It's also important that after the <code>using</code> statement, and another <code>Resolve&lt;IDisposableObject&gt;</code> is called, it returns a <em>new</em> instance.</p>