I want to have a service like the following
public SomeService(IMongoDatabase mongoDatabase) {
DB = mongoDatabase;
}
and I want to use a factory to resolve IMongoDatabase, just to encapsulate the IConfiguration usage
public static IMongoDatabase GetMongoDatabase(IConfiguration config)
{
var connectionString = config.Get("SomeConnectionString");
// MongoClient handles connection pooling internally
var client = new MongoClient(connectionString);
var db = client.GetDatabase(config.Get("SomeDbName"));
return db;
}
I can't figure out how to handle the registrations so that MongoDbFactory.GetMongoDatabase gets called whenever any class needs an IMongoDatabase. IConfiguration will be registered already.
I'd really like to just use an IMongoDatabase and not a Func<IConfiguration, IMongoDatabase> in my Service. The latter just seems way too obtuse, requiring consumers to implement steps that I should be able to implement for them.