title: .NET core - Dependency Injection, factories and IDisposable
body: <p>I'm investigating a memory leak in my application. Here's the context:</p>
  <p>Let's assume I have to process different types of XMLs files and I receive a large amount of XML files per day, so I have a <code>IXmlProcessor</code> interface .</p>
  <pre><code>public interface IXmlProcessor
  {
       void ProcessXml(string xml);
  }
  </code></pre>
  <p>And some concrete XMLProcessors.</p>
  <pre><code>public class UserXmlProcessor : IXmlProcessor
  {
       private readonly IUserRepository _userRepository;
  
       public UserXmlProcessor(IUserRepository userRepository)
       {
             _userRepository = userRepository;
       }
  
       public void ProcessXml(string xml)
       {
             // do something with the xml
             // call _userRepository 
       }
   }
  </code></pre>
  <p>All <code>IXmlProcessor</code> concrete types are registered to the DI Container, and to resolve them I have a Factory class, that's also registered to the DI container, something like this:</p>
  <pre><code>public class XmlProcessorFactory where TType : class
  {
      private readonly IServiceProvider _serviceProvider;
  
      public XmlProcessorFactory(IServiceProvider serviceProvider)
      {
          _serviceProvider = serviceProvider;
      }
  
      public IXmlProcessor GetImplementation(string identifier)
      {
          var type = FindType(identifier);
  
          return _serviceProvider.GetService(type) as IXmlProcessor;
      }
  
      private Type FindType(string identifier)
      {
          // do some reflection to find the type based on the identifier (UserXmlProcessor, for example)
          // don't worry, there's caching to avoid unecessary reflection
      }
  }
  </code></pre>
  <p>And at some point I call them all:</p>
  <pre><code>public class WorkItem
  {
      public string Identifier { get; set; }
      public string Xml { get; set; }
  }
  
  public class WorkingClass
  {
  
      private readonly XmlProcessorFactory _xmlProcessorFactory;
  
      public WorkingClass(XmlProcessorFactory xmlProcessorFactory)
      {
          _xmlProcessorFactory = xmlProcessorFactory;
      }
  
      public void DoWork(WorkItem item)
      {
          var processor = _xmlProcessorFactory.GetImplementation(item.Identifier);
          processor.ProcessXml(item.Xml);
      }
  }
  </code></pre>
  <p><code>IUserRepository</code> is a straightforward implementation, with an Entity Framwork context.</p>
  <p>So, here's the problem: according to the Microsoft <a href="https://docs.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines" rel="nofollow noreferrer">documentation</a>:</p>
  <blockquote>
  <p>Services resolved from the container should never be disposed by the developer.</p>
  </blockquote>
  <blockquote>
  <p>Receiving an IDisposable dependency via DI doesn't require that the receiver implement IDisposable itself. The receiver of the IDisposable dependency shouldn't call Dispose on that dependency.</p>
  </blockquote>
  <p>So if I inject IUserRepository to a controller, that's fine, the container will handle the object's disposal as well as the EF Context's disposal, none needs to be IDisposable.</p>
  <p>But what about my Xml Processors? The documentation says:</p>
  <blockquote>
  <p><strong>Services not created by the service container</strong></p>
  </blockquote>
  <blockquote>
  <p>The developer is responsible for disposing the services.</p>
  </blockquote>
  <blockquote>
  <p>Avoid using the service locator pattern. For example, don't invoke GetService to obtain a service instance when you can use DI instead.
  Another service locator variation to avoid is injecting a factory that resolves dependencies at runtime. Both of these practices mix Inversion of Control strategies.</p>
  </blockquote>
  <p>And also <code>_ = serviceProvider.GetRequiredService&lt;ExampleDisposable&gt;();</code> being an anti-pattern. But as you can see I <strong>do</strong> need to resolve the dependency at runtime, based on the XML Identifier and I dont' want to resort to switch cases.</p>
  <p>So:</p>
  <ul>
  <li>Should the IXmlProcessors implement IDisposable and release IUserRepository manually?</li>
  <li>Should I also cascade and make IUserRepository implement IDisposable to release EntityContext?</li>
  <li>If so, wouldn't that affect the service lifetime if it's injected in a controller?</li>
  </ul>