Home > other >  ASP.NET Core Web API and MongoDB with multiple Collections and multiple Databases
ASP.NET Core Web API and MongoDB with multiple Collections and multiple Databases

Time:02-23

With this question ASP.NET Core Web API and MongoDB with multiple Collections answers I can see the perfect use of singleton and lazy initialization for mongodb's WITH single database and multiple collection.

Startup.cs

public void ConfigureServices(IServiceCollection services)
    {
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "Asset Model API", Version = "v1" });
        });

        ConfigureMongoDb(services);

        services.AddControllers()
            .AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
    }

    private void ConfigureMongoDb(IServiceCollection services)
    {
        var settings = GetMongoDbSettings();
        services.AddSingleton(_ => CreateMongoDatabase(settings));

        AddMongoDbService<AuthorService, Author>(settings.AuthorsCollectionName);
        AddMongoDbService<BookService, Book>(settings.BooksCollectionName);

        void AddMongoDbService<TService, TModel>(string collectionName)
        {
            services.AddSingleton(sp => sp.GetRequiredService<IMongoDatabase>().GetCollection<TModel>(collectionName));
            services.AddSingleton(typeof(TService));
        }
    }

    private DatabaseSettings GetMongoDbSettings() =>
        Configuration.GetSection(nameof(DatabaseSettings)).Get<DatabaseSettings>();

    private static IMongoDatabase CreateMongoDatabase(DatabaseSettings settings)
    {
        var client = new MongoClient(settings.ConnectionString);
        return client.GetDatabase(settings.DatabaseName);
    }

BookService.cs

public class BookService
{
    private readonly IMongoCollection<Book> _books;

    public BookService(IMongoCollection<Book> books)
    {
        _books = books;
    }

    public List<Book> Get() => _books.Find(book => true).ToList();
}

BooksController.cs

public class BooksController : ControllerBase
{
    private readonly BookService _bookService;

    public BooksController(BookService bookService)
    {
        _bookService = bookService;
    }

    [HttpGet]
    public ActionResult<List<Book>> Get() => _bookService.Get();
}

My use case: I have multiple databases and my API end point will accept database name as ONE of the argument

  [HttpGet]
    public ActionResult<List<Book>> Get(string dbName) => _bookService.Get(dbName);

Now question is what changes requires in my service class and ConfigureMongoDb method of startup class so that I can still get lazy initialization and singleton support?

CodePudding user response:

Instead of injecting the IMongoCollection into the services (BooksService, ...), you could create a class that can provide the collection based upon the database name, e.g.

public class MongoCollectionProvider
{
  private readonly IMongoClient _client;

  public MongoDbCollectionProvider(IMongoClient client) 
  {
    _client = client;
  }

  public IMongoCollection<T> GetCollection<T>(string database, string collection)
  {
    var db = _client.GetDatabase(database);
    return db.GetCollection<T>(collection);
  }
}

You can then inject this class into the services and retrieve the collection before querying the database, e.g.

public class BookService
{
    private readonly MongoCollectionProvider _prov;
    private readonly string _coll;

    public BookService(MongoCollectionProvider prov, string coll)
    {
        _prov = prov;
        _coll = coll;
    }

    public List<Book> Get(string dbName) => _prov.GetCollection<Book>(dbName, _coll).Find(book => true).ToList();
}

You can register the services like this:

private void ConfigureMongoDb(IServiceCollection services)
{
  var settings = GetMongoDbSettings();
  services.AddSingleton(_ => CreateMongoClient(settings));
  services.AddSingleton<MongoCollectionProvider>();

  services.AddSingleton(sp => 
  {
    var prov = sp.GetRequiredService<MongoCollectionProvider>();
    return new AuthorsService(prov, settings.AuthorsCollectionName);
  }
  services.AddSingleton(sp => 
  {
    var prov = sp.GetRequiredService<MongoCollectionProvider>();
    return new BooksService(prov, settings.BooksCollectionName);
  }
}

private DatabaseSettings GetMongoDbSettings() => Configuration.GetSection(nameof(DatabaseSettings)).Get<DatabaseSettings>();

private static IMongoDatabase CreateMongoClient(DatabaseSettings settings)
{
  return new MongoClient(settings.ConnectionString);
}

If you want to constrain the databases to specific ones, you can change the MongoCollectionProvider to only accept registered database names.

  • Related