66 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			66 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Core.Blueprint.Mongo;
 | |
| using Core.Blueprint.Mongo.Configuration;
 | |
| using Microsoft.Extensions.Configuration;
 | |
| using Microsoft.Extensions.DependencyInjection;
 | |
| using Microsoft.Extensions.Options;
 | |
| using MongoDB.Driver;
 | |
| 
 | |
| namespace Core.Blueprint.DAL.Mongo.Configuration
 | |
| {
 | |
|     /// <summary>
 | |
|     /// The <see cref="RegisterBlueprint"/> class contains extension methods for registering the MongoDB context and configuration settings 
 | |
|     /// to the <see cref="IServiceCollection"/> in the dependency injection container.
 | |
|     /// </summary>
 | |
|     public static class RegisterBlueprint
 | |
|     {
 | |
|         /// <summary>
 | |
|         /// Adds the MongoDB layer services to the <see cref="IServiceCollection"/>.
 | |
|         /// Registers the MongoDB context and configuration settings for MongoDB connection, database name, and audience.
 | |
|         /// </summary>
 | |
|         /// <param name="services">The <see cref="IServiceCollection"/> to which the services will be added.</param>
 | |
|         /// <param name="configuration">The <see cref="IConfiguration"/> used to load MongoDB settings.</param>
 | |
|         /// <returns>The updated <see cref="IServiceCollection"/> with MongoDB services registered.</returns>
 | |
|         public static IServiceCollection AddMongoLayer(this IServiceCollection services, IConfiguration configuration)
 | |
|         {
 | |
|             var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty;
 | |
| 
 | |
|             services.AddSingleton<IMongoContext, MongoContext>();
 | |
| 
 | |
|             var ConnectionString = configuration.GetSection("ConnectionStrings:MongoDB").Value ?? string.Empty;
 | |
|             var Databasename = configuration.GetSection("MongoDb:DatabaseName").Value ?? string.Empty;
 | |
|             var Audience = (environment == "Local")
 | |
|                 ? configuration.GetSection("MongoDb:LocalAudience").Value
 | |
|                 : configuration.GetSection("MongoDb:Audience").Value;
 | |
| 
 | |
|             if (string.IsNullOrEmpty(ConnectionString) || string.IsNullOrEmpty(Databasename) || string.IsNullOrEmpty(Audience))
 | |
|                 throw new InvalidOperationException("Mongo connection is not configured correctly.");
 | |
| 
 | |
|             services.Configure<MongoDbSettings>(options =>
 | |
|             {
 | |
|                 options.ConnectionString = ConnectionString;
 | |
|                 options.Databasename = Databasename;
 | |
|                 options.Audience = Audience;
 | |
|             });
 | |
| 
 | |
|             services.AddSingleton<IMongoClient>(serviceProvider =>
 | |
|             {
 | |
|                 var settings = serviceProvider.GetRequiredService<IOptions<MongoDbSettings>>().Value;
 | |
|                 var mongoClientSettings = MongoClientSettings.FromConnectionString(settings.ConnectionString);
 | |
|                 mongoClientSettings.Credential = MongoCredential.CreateOidcCredential(new AzureIdentityProvider(settings.Audience));
 | |
|                 return new MongoClient(mongoClientSettings);
 | |
|             });
 | |
| 
 | |
|             services.AddSingleton<IMongoDatabase>(serviceProvider =>
 | |
|             {
 | |
|                 var settings = serviceProvider.GetRequiredService<IOptions<MongoDbSettings>>().Value;
 | |
|                 var client = serviceProvider.GetRequiredService<IMongoClient>();
 | |
|                 return client.GetDatabase(settings.Databasename);
 | |
|             });
 | |
| 
 | |
|             services.AddSingleton<IMongoDbSettings>(serviceProvider => serviceProvider.GetRequiredService<IOptions<MongoDbSettings>>().Value);
 | |
| 
 | |
|             return services;
 | |
|         }
 | |
|     }
 | |
| }
 | 
