title: Dependency Injection of MongoDB Connection Information in View Layer of MVC
body: <p>I'm trying to get familiar with dependency injection in a layered solution that uses .Net Core's MVC and a Data Layer to process MongoDB information following this tutorial: <a href="https://code-maze.com/getting-started-aspnetcore-mongodb/" rel="nofollow noreferrer">https://code-maze.com/getting-started-aspnetcore-mongodb/</a></p>
  <p>My .sln is set up like this:</p>
  <p>Project A ---- MVC Layer</p>
  <p>Project B ---- MongoDB Layer</p>
  <p>What I want to do is use a function I defined in the DB Layer in the view to display the information of a document. The problem I'm having is figuring out how to call the function in the Index .cshtml.</p>
  <p>Following the tut, I started by making an interface to pull the connection information from services. Since A depends on B, I wrote this in B:</p>
  <pre><code>namespace DataLayer
  {
      public class MyDBSettings : IDotNetStudyDBSettings
      {
          public string FileHistoryCollectioName { get; set; }
          public string FileResultsCollectionName { get; set; }
          public string ConnectionString { get; set; }
  
          public string DatabaseName { get; set; }
  
  
  
      }
  
  
      public interface IDotNetStudyDBSettings
      {
          string FileHistoryCollectioName { get; set; }
          string FileResultsCollectionName { get; set; }
          string ConnectionString { get; set; }
          string DatabaseName { get; set; }
  
  
  
      }
  }
  </code></pre>
  <p>I configured my services in my Startup.cs to pass that information into this interface. Per the tutorial I also made entity models for the collections I'm trying to pull:</p>
  <pre><code>  public class File_History
      {
          [BsonId]
          [BsonRepresentation(BsonType.ObjectId)]
  
          public string Id { get; set; }
          public DateTime Date { get; set; }
          public string FileName { get; set; }
  
         
  
      }
  
      public class File_Result
      {
          [BsonId]
          [BsonRepresentation(BsonType.ObjectId)]
  
          public string Id { get; set; }
          public int Attempt { get; set; }
          public string CF_Output { get; set; }
  
          public string UR_Output { get; set; }
  
          
  }
  </code></pre>
  <p>To try to retrieve the collection, in the .cshtml I instantiated the MyDBSettings class and passed it to the function I wrote to retrieve Mongo stuff:</p>
  <pre><code>public class MongoDB_Communicator
      {
           IMongoCollection&lt;File_History&gt; _filehistory;
  
          public IMongoCollection&lt;File_History&gt; FileService(IDotNetStudyDBSettings settings)
          {
              var client = new MongoClient(settings.ConnectionString);
              var database = client.GetDatabase(settings.DatabaseName);
              _filehistory = database.GetCollection&lt;File_History&gt;(settings.FileHistoryCollectioName);
              return (_filehistory);
  
          }
      }
  </code></pre>
  <p>Here's the .cshtml I ultimately wrote:</p>
  <pre><code>@{ 
  
      DataLayer.MyDBSettings test_sets = new DataLayer.MyDBSettings();
      MongoDB_Communicator test_cm = new MongoDB_Communicator();
    
  
  }
  
  @{
      
      ViewData[&quot;Title&quot;] = &quot;Home Page&quot;;
  }
  
  &lt;div class=&quot;text-center&quot;&gt;
      &lt;h1 class=&quot;display-4&quot;&gt;Welcome&lt;/h1&gt;
      &lt;p&gt;Drag Python (3) File Below. No funny business or you're out.&lt;/p&gt;
      &lt;p&gt;
      
      
      @{
          test_cm.FileService(test_sets);
  
  
  
  
  
          }&lt;/p&gt;
  
  &lt;/div&gt;
  </code></pre>
  <p>This blew up, giving me the following error message:</p>
  <p><a href="https://i.stack.imgur.com/HnbwD.png" rel="nofollow noreferrer">Me being a smooth brained dork</a></p>
  <p>EDIT: I do have it configured in my appsetting.Json as follows (edited to remove login stuff)</p>
  <p><a href="https://i.stack.imgur.com/Iqy47.png" rel="nofollow noreferrer">Appsettings</a></p>
  <p><strong>My question is, when it comes time to actually retrieve the information, what is the syntax I should be using to retrieve that setting information from the interface I made?</strong></p>
  <p>I have the following set up in my StartUp.cs - I was under the impression that by adding that class to the services it would be populated once instantiated.</p>
  <pre><code>public void ConfigureServices(IServiceCollection services)
          {
              //notate all of this
              services.Configure&lt;MyDBSettings&gt;(Configuration.GetSection(nameof(MyDBSettings)));
              services.AddSingleton&lt;IDotNetStudyDBSettings&gt;(provider =&gt; provider.GetRequiredService&lt;IOptions&lt;MyDBSettings&gt;&gt;().Value);
              services.AddControllersWithViews();
              services.AddControllers();
              services.AddScoped&lt;MongoDB_Communicator&gt;();
          }
  </code></pre>
  <p>I feel like there's a conceptual gap here: any help would be appreciated!</p>