public static void AddMongoDB(this IServiceCollection services, IConfiguration configuration, string configSectionKey) { services.AddSingleton(context => { var options = new MongoDbOptions(); configuration.GetSection(configSectionKey).Bind(options); return(options); }); services.AddSingleton <MongoClient>(ctx => { var options = ctx.GetService <MongoDbOptions>(); return(new MongoClient(options.ConnectionString)); }); services.AddScoped <IMongoDatabase>(ctx => { var options = ctx.GetService <MongoDbOptions>(); var client = ctx.GetService <MongoClient>(); return(client.GetDatabase(options.Database)); }); services.AddScoped <IMongoDbSeeder, MongoDbSeeder>(); services.AddScoped <IDbInitializer, MongoDbInitializer>(); }
public MongoDbInitializer(IMongoDatabase database, IMongoDbSeeder seeder, MongoDbOptions options) { _database = database; _seeder = seeder; _seed = options.Seed; }