67 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Microsoft.Extensions.Configuration;
 | |
| 
 | |
| namespace Core.Blueprint.Mongo
 | |
| {
 | |
|     /// <summary>
 | |
|     /// The <see cref="MongoContext"/> class represents the MongoDB context that contains the connection information, 
 | |
|     /// including the connection string, database name, and audience.
 | |
|     /// It implements the <see cref="IMongoContext"/> interface to provide methods for accessing these values.
 | |
|     /// </summary>
 | |
|     public sealed class MongoContext : IMongoContext
 | |
|     {
 | |
|         /// <summary>
 | |
|         /// Gets or sets the connection string used to connect to the MongoDB instance.
 | |
|         /// </summary>
 | |
|         public string ConnectionString { get; set; } = string.Empty;
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Gets or sets the name of the MongoDB database.
 | |
|         /// </summary>
 | |
|         public string Databasename { get; set; } = string.Empty;
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Gets or sets the audience (resource identifier) used for MongoDB authentication.
 | |
|         /// </summary>
 | |
|         public string Audience { get; set; } = string.Empty;
 | |
| 
 | |
|         private readonly IConfiguration configuration;
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Initializes a new instance of the <see cref="MongoContext"/> class using the provided <see cref="IConfiguration"/>.
 | |
|         /// The configuration is used to retrieve MongoDB connection settings.
 | |
|         /// </summary>
 | |
|         /// <param name="configuration">The configuration used to retrieve the MongoDB connection settings.</param>
 | |
|         public MongoContext(IConfiguration configuration)
 | |
|         {
 | |
|             this.configuration = configuration;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Retrieves the MongoDB connection string from the configuration.
 | |
|         /// </summary>
 | |
|         /// <returns>The MongoDB connection string, or an empty string if not found.</returns>
 | |
|         public string GetConnectionString()
 | |
|         {
 | |
|             return configuration.GetConnectionString("MongoDb:ConnectionString")?.ToString() ?? string.Empty;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Retrieves the MongoDB database name from the configuration.
 | |
|         /// </summary>
 | |
|         /// <returns>The MongoDB database name, or an empty string if not found.</returns>
 | |
|         public string GetDatabasename()
 | |
|         {
 | |
|             return configuration.GetSection("MongoDb:DatabaseName").Value ?? string.Empty;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Retrieves the MongoDB audience (resource identifier) from the configuration.
 | |
|         /// </summary>
 | |
|         /// <returns>The MongoDB audience, or an empty string if not found.</returns>
 | |
|         public string GetAudience()
 | |
|         {
 | |
|             return configuration.GetSection("MongoDb:Audience").Value ?? string.Empty;
 | |
|         }
 | |
|     }
 | |
| }
 | 
