Dependency Injection of MongoDB Connection Information in View Layer of MVC

Viewed 2686

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: https://code-maze.com/getting-started-aspnetcore-mongodb/

My .sln is set up like this:

Project A ---- MVC Layer

Project B ---- MongoDB Layer

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.

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:

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; }



    }
}

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:

  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; }

        
}

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:

public class MongoDB_Communicator
    {
         IMongoCollection<File_History> _filehistory;

        public IMongoCollection<File_History> FileService(IDotNetStudyDBSettings settings)
        {
            var client = new MongoClient(settings.ConnectionString);
            var database = client.GetDatabase(settings.DatabaseName);
            _filehistory = database.GetCollection<File_History>(settings.FileHistoryCollectioName);
            return (_filehistory);

        }
    }

Here's the .cshtml I ultimately wrote:

@{ 

    DataLayer.MyDBSettings test_sets = new DataLayer.MyDBSettings();
    MongoDB_Communicator test_cm = new MongoDB_Communicator();
  

}

@{
    
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Drag Python (3) File Below. No funny business or you're out.</p>
    <p>
    
    
    @{
        test_cm.FileService(test_sets);





        }</p>

</div>

This blew up, giving me the following error message:

Me being a smooth brained dork

EDIT: I do have it configured in my appsetting.Json as follows (edited to remove login stuff)

Appsettings

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?

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.

public void ConfigureServices(IServiceCollection services)
        {
            //notate all of this
            services.Configure<MyDBSettings>(Configuration.GetSection(nameof(MyDBSettings)));
            services.AddSingleton<IDotNetStudyDBSettings>(provider => provider.GetRequiredService<IOptions<MyDBSettings>>().Value);
            services.AddControllersWithViews();
            services.AddControllers();
            services.AddScoped<MongoDB_Communicator>();
        }

I feel like there's a conceptual gap here: any help would be appreciated!

1 Answers

The section name in your appsettings.json must match the section you want to configure here

 services.Configure<MyDBSettings>(Configuration.GetSection("DotNetCore_Study"));
Related