Adithyavj_SuggestionApp/SuggestionAppLibrary/DataAccess/DbConnection.cs
using Microsoft.Extensions.Configuration;

namespace SuggestionAppLibrary.DataAccess;

// Sets up connection once..
public class DbConnection : IDbConnection
{
    private readonly IConfiguration _config;
    private readonly IMongoDatabase _db;
    private string _connectionId = "MongoDB"; // referencing the Entry in connectionStrings in appsettings.json
    // properties for accessing the collection sets in the Database
    public string DbName { get; private set; }
    public string CategoryCollectionName { get; private set; } = "categories";
    public string StatusCollectionName { get; private set; } = "statuses";
    public string UserCollectionName { get; private set; } = "users";
    public string SuggestionCollectionName { get; private set; } = "suggestions";
    // connection to our tables/ collections
    public MongoClient Client { get; private set; }
    public IMongoCollection<CategoryModel> CategoryCollection { get; private set; }
    public IMongoCollection<StatusModel> StatusCollection { get; private set; }
    public IMongoCollection<UserModel> UserCollection { get; private set; }
    public IMongoCollection<SuggestionModel> SuggestionCollection { get; private set; }

    public DbConnection(IConfiguration config)
    {
        _config = config;
        // configuring new client
        Client = new MongoClient(_config.GetConnectionString(_connectionId));
        // connection to our database
        DbName = _config["DatabaseName"];
        _db = Client.GetDatabase(DbName);

        // configuring connection to all the 4 collections
        CategoryCollection = _db.GetCollection<CategoryModel>(CategoryCollectionName);
        StatusCollection = _db.GetCollection<StatusModel>(StatusCollectionName);
        UserCollection = _db.GetCollection<UserModel>(UserCollectionName);
        SuggestionCollection = _db.GetCollection<SuggestionModel>(SuggestionCollectionName);
    }
}
