title: Passing Services using Dependency Injection and Factory Pattern in ASP.NET
body: <p>I am using ASP.NET Core, I know that such Logging mechanism is already provided by the framework, but using this to illustrate my problem.</p>
  
  <p>I am using kind of Factory pattern to build the Logger class, since I don't know the type of logging (because it is stored in DB).</p>
  
  <p>The ILogger Contract</p>
  
  <pre><code>Log(string msg)
  </code></pre>
  
  <p>Then LoggerFactory will return an ILogger after creating a Logger based on param passed from DB:</p>
  
  <pre><code>public class LoggerFactory
  {
      public static Contracts.ILogger BuildLogger(LogType type)
      {
          return GetLogger(type);
      }
  
  //other code is omitted, GetLogger will return an implementation of the related logger
  </code></pre>
  
  <p>Now, when I need to use the Logger I have to do it in this way:</p>
  
  <pre><code>  public class MyService
  {
      private ILogger _logger
      public MyService()
      {
          _logger = LoggerFactory.BuildLogger("myType");
      }
  </code></pre>
  
  <p>But, I intend to keep my classes without any instantiation, I need to use Constructor DI in MyService and I need to inject all the dependencies on Startup:</p>
  
  <pre><code>    services.AddTransient&lt;Contracts.ILogger, LoggerFactory.BuildLogger("param") &gt; ();
  </code></pre>
  
  <p>But this will not work this we need to pass a concrete implementation. 
  How to make that work using DI, is there a better approach for implementing that?</p>