title: Dependency Injection with .NETCore for DAL and connection string
body: <p>I am new to the DI patterns with .NETCore, and I am having trouble getting my connection strings to my DAL.</p>
  
  <p>I followed the advice given in <a href="https://stackoverflow.com/questions/35015066/passing-applications-connection-string-down-to-a-repository-class-library-in-as/35064386#35064386">this thread</a> via the accepted answer, and subsequent comments.</p>
  
  <p>This is my base class</p>
  
  <pre><code>public class BaseRepository : IRepository&lt;IDataModel&gt;
  {
      private readonly IConfiguration config;
  
      public BaseRepository(IConfiguration config)
      {
          this.config = config;
      }
  
      public string GetSQLConnectionString()
      {
          return config["Data:DefaultConnetion:ConnectionString"];
      }
  </code></pre>
  
  <p>This is a snippet of a repository class inheriting the base class</p>
  
  <pre><code>public class PrivacyLevelRepository : BaseRepository, IRepository&lt;PrivacyLevelDM&gt;
  {
      public PrivacyLevelRepository(IConfiguration config) : base(config) { }
      public void Add(PrivacyLevelDM dataModel)
      {
           ...
      }
  }
  </code></pre>
  
  <p>This is in my startup.cs</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
      {
          // Add framework services.
          services.AddMvc();
          services.AddScoped&lt;IRepository&lt;IDataModel&gt;&gt;(c =&gt; new BaseRepository(Configuration));
      }
  </code></pre>
  
  <p>However, In my service layer, the instantiation of the repository class is still asking for the (IConfiguration config) to be passed as a parameter.</p>
  
  <pre><code> PrivacyLevelRepository repo = new PrivacyLevelRepository();
  </code></pre>
  
  <p>How do I get the IConfiguration loaded directly to my DAL, without having to pass it from Controller > BLL > DAL.  This seems extremely inefficient, and incorrect.  As the DAL should be determining the connection for an object, not the controller or service layer.  They should be agnostic of the datasource, no?</p>
  
  <p>I figure this is something simple, that I am just not seeing within the DI/IoC paradigm, but I cannot figure it out.</p>
  
  <p>Edit: I am not using Entity Framework, but a custom Data layer.</p>
  
  <p>Thanx for any help.</p>