-
Notifications
You must be signed in to change notification settings - Fork 3
Open
Description
@MohanedZekry, I was just going through the project uses MongoDB with a clean architecture, but I did not see an explicit singleton registration for IMongoClient, and I did not find an index creation step for collections like Users. Please correct me if I missed it.
The thing is basically:
- MongoClient is thread safe and should be single instance per app to reuse connection pools and reduce overhead
- Creating indexes at startup avoids silent duplicates and gives a clear error from the database which is easy to handle
I would suggest a changes:
- DI lifetimes
builder.Services.AddSingleton<IMongoClient>(_ =>
new MongoClient(builder.Configuration.GetConnectionString("MongoDb")));
builder.Services.AddScoped(sp =>
sp.GetRequiredService<IMongoClient>().GetDatabase(
builder.Configuration["MongoDB:DatabaseName"] ?? "appdb"));
- Create indexes during app start in an idempotent way
using var scope = app.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<IMongoDatabase>();
var users = db.GetCollection<User>("Users");
var emailIndex = new CreateIndexModel<User>(
Builders<User>.IndexKeys.Ascending(u => u.Email),
new CreateIndexOptions { Unique = true });
await users.Indexes.CreateOneAsync(emailIndex);
You can also test it via
- Try two parallel creates with the same email
- Before change you may get duplicate records or late failures in app logic
- After change Mongo will return a duplicate key error directly which can be handled in a simple way
Let me know if I can raise a PR that adds the DI update and simple index creation code.
Metadata
Metadata
Assignees
Labels
No labels