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?