| | | 1 | | using ArturRios.Data.MongoDb.Configuration; |
| | | 2 | | using ArturRios.Data.MongoDb.Interfaces; |
| | | 3 | | using ArturRios.Data.MongoDb.Repositories; |
| | | 4 | | using ArturRios.Data.MongoDb.Transactions; |
| | | 5 | | using Microsoft.Extensions.Configuration; |
| | | 6 | | using Microsoft.Extensions.DependencyInjection; |
| | | 7 | | using MongoDB.Driver; |
| | | 8 | | |
| | | 9 | | namespace ArturRios.Data.MongoDb.DependencyInjection; |
| | | 10 | | |
| | | 11 | | /// <summary>Dependency-injection registration for the ArturRios.Data.MongoDb document store.</summary> |
| | | 12 | | public static class ServiceCollectionExtensions |
| | | 13 | | { |
| | | 14 | | /// <param name="services">The service collection.</param> |
| | | 15 | | extension(IServiceCollection services) |
| | | 16 | | { |
| | | 17 | | /// <summary>Registers the MongoDB document store, binding options from configuration.</summary> |
| | | 18 | | /// <param name="configuration">Application configuration.</param> |
| | | 19 | | /// <param name="sectionName">Configuration section holding the options. Defaults to "ArturRios.Data.MongoDb".</ |
| | | 20 | | public IServiceCollection AddMongoData(IConfiguration configuration, |
| | | 21 | | string sectionName = "ArturRios.Data.MongoDb") |
| | 0 | 22 | | { |
| | 0 | 23 | | var options = configuration.GetSection(sectionName).Get<MongoOptions>() ?? new MongoOptions(); |
| | 0 | 24 | | return services.AddMongoData(options); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary>Registers the MongoDB document store from an explicit options instance.</summary> |
| | | 28 | | /// <param name="options">The Mongo options.</param> |
| | | 29 | | public IServiceCollection AddMongoData(MongoOptions options) |
| | 1 | 30 | | { |
| | 2 | 31 | | services.AddSingleton<IMongoClient>(_ => new MongoClient(options.ConnectionString)); |
| | 1 | 32 | | services.AddScoped<IMongoDatabase>(sp => |
| | 2 | 33 | | sp.GetRequiredService<IMongoClient>().GetDatabase(options.DatabaseName)); |
| | 1 | 34 | | services.AddScoped<MongoContext>(); |
| | | 35 | | |
| | 1 | 36 | | services.AddScoped(typeof(IDocumentReadOnlyRepository<>), typeof(MongoDocumentRepository<>)); |
| | 1 | 37 | | services.AddScoped(typeof(IDocumentRepository<>), typeof(MongoDocumentRepository<>)); |
| | 1 | 38 | | services.AddScoped(typeof(IAsyncDocumentReadOnlyRepository<>), typeof(MongoDocumentRepository<>)); |
| | 1 | 39 | | services.AddScoped(typeof(IAsyncDocumentRepository<>), typeof(MongoDocumentRepository<>)); |
| | | 40 | | |
| | 1 | 41 | | services.AddScoped<IMongoUnitOfWork, MongoUnitOfWork>(); |
| | 1 | 42 | | services.AddScoped<IAsyncMongoUnitOfWork, MongoUnitOfWork>(); |
| | | 43 | | |
| | 1 | 44 | | return services; |
| | | 45 | | } |
| | | 46 | | } |
| | | 47 | | } |