using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace BuildingBlocks.Mongo { using Web; public static class Extensions { public static IServiceCollection AddMongoDbContext( this IServiceCollection services, IConfiguration configuration, Action? configurator = null) where TContext : MongoDbContext { return services.AddMongoDbContext(configuration, configurator); } public static IServiceCollection AddMongoDbContext( this IServiceCollection services, IConfiguration configuration, Action? configurator = null) where TContextService : IMongoDbContext where TContextImplementation : MongoDbContext, TContextService { services.Configure(configuration.GetSection(nameof(MongoOptions))); if (configurator is { }) { services.Configure(nameof(MongoOptions), configurator); } else { services.AddValidateOptions(); } services.AddScoped(typeof(TContextService), typeof(TContextImplementation)); services.AddScoped(typeof(TContextImplementation)); services.AddScoped(sp => sp.GetRequiredService()); services.AddTransient(typeof(IMongoRepository<,>), typeof(MongoRepository<,>)); services.AddTransient(typeof(IMongoUnitOfWork<>), typeof(MongoUnitOfWork<>)); return services; } } }