有了這個問題ASP.NET Core Web API and MongoDB with multiple Collections答案,我可以看到單例和延遲初始化對 mongodb 的WITH 單個資料庫和多個集合的完美使用。
啟動.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);
}
圖書服務.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();
}
我的用例:我有多個資料庫,我的 API 端點將接受database name作為引數之一
[HttpGet]
public ActionResult<List<Book>> Get(string dbName) => _bookService.Get(dbName);
現在的問題是我的service類和ConfigureMongoDb啟動類的方法需要進行哪些更改,以便我仍然可以獲得延遲初始化和單例支持?
uj5u.com熱心網友回復:
您可以創建一個可以根據資料庫名稱提供集合的類,而不是將 IMongoCollection 注入服務(BooksService,...),例如
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);
}
}
然后,您可以將此類注入服務并在查詢資料庫之前檢索集合,例如
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();
}
您可以像這樣注冊服務:
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);
}
如果要將資料庫限制為特定的資料庫,可以將 MongoCollectionProvider 更改為僅接受已注冊的資料庫名稱。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/431756.html
標籤:C# mongodb asp.net 核心 依赖注入 单身人士
