Documentation Coverage Report
Current view: top level - Cqrs.Mongo/Factories - MongoDataStoreConnectionStringFactory.cs Hit Total Coverage
Version: 2.2 Artefacts: 4 4 100.0 %
Date: 2017-09-22

          Line data    Source code
       1             : #region Copyright
       2             : // // -----------------------------------------------------------------------
       3             : // // <copyright company="Chinchilla Software Limited">
       4             : // //   Copyright Chinchilla Software Limited. All rights reserved.
       5             : // // </copyright>
       6             : // // -----------------------------------------------------------------------
       7             : #endregion
       8             : 
       9             : using System;
      10             : using System.Configuration;
      11             : using Cqrs.Configuration;
      12             : using cdmdotnet.Logging;
      13             : using Cqrs.Exceptions;
      14             : 
      15             : namespace Cqrs.Mongo.Factories
      16             : {
      17             :         /// <summary>
      18             :         /// A factory for MongoDb related connection string settings.
      19             :         /// </summary>
      20             :         public class MongoDataStoreConnectionStringFactory : IMongoDataStoreConnectionStringFactory
      21           1 :         {
      22             :                 /// <summary>
      23             :                 /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the name of the connection string of the MongoDB server.
      24             :                 /// </summary>
      25             :                 public static string MongoDbConnectionStringKey = "Cqrs.MongoDb.DataStore.ConnectionStringName";
      26             : 
      27             :                 /// <summary>
      28             :                 /// Backwards compatibility with version 1.
      29             :                 /// </summary>
      30             :                 public static string OldMongoDbConnectionStringKey = "CqrsMongoDb";
      31             : 
      32             :                 /// <summary>
      33             :                 /// Backwards compatibility with version 1.
      34             :                 /// </summary>
      35             :                 public static string OldMongoDbDatabaseNameKey = "CqrsMongoDbDatabaseName";
      36             : 
      37             :                 /// <summary>
      38             :                 /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the name of the database.
      39             :                 /// </summary>
      40             :                 public static string MongoDbDatabaseNameKey = "Cqrs.MongoDb.DataStore.DatabaseName";
      41             : 
      42             :                 /// <summary>
      43             :                 /// Gets or sets the <see cref="IConfigurationManager"/>.
      44             :                 /// </summary>
      45             :                 protected IConfigurationManager ConfigurationManager { get; private set; }
      46             : 
      47             :                 /// <summary>
      48             :                 /// Gets or sets the <see cref="ILogger"/>.
      49             :                 /// </summary>
      50             :                 protected ILogger Logger { get; private set; }
      51             : 
      52             :                 /// <summary>
      53             :                 /// Instantiate a new instance of <see cref="MongoDataStoreConnectionStringFactory"/>.
      54             :                 /// </summary>
      55           1 :                 public MongoDataStoreConnectionStringFactory(IConfigurationManager configurationManager, ILogger logger)
      56             :                 {
      57             :                         ConfigurationManager = configurationManager;
      58             :                         Logger = logger;
      59             :                 }
      60             : 
      61             :                 /// <summary>
      62             :                 /// Get the connection string for the MongoDB server.
      63             :                 /// </summary>
      64           1 :                 public string GetMongoConnectionString()
      65             :                 {
      66             :                         Logger.LogInfo("Getting MongoDB connection string", "MongoDataStoreConnectionStringFactory\\GetMongoConnectionString");
      67             :                         try
      68             :                         {
      69             :                                 string applicationKey;
      70             :                                 if (ConfigurationManager.TryGetSetting(MongoDbConnectionStringKey, out applicationKey) && !string.IsNullOrEmpty(applicationKey))
      71             :                                 {
      72             :                                         try
      73             :                                         {
      74             :                                                 ConnectionStringSettings connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[applicationKey];
      75             :                                                 if (connectionString != null)
      76             :                                                         return connectionString.ConnectionString;
      77             :                                         }
      78             :                                         catch (Exception exception)
      79             :                                         {
      80             :                                                 throw new MissingConnectionStringException(applicationKey, exception);
      81             :                                         }
      82             :                                 }
      83             :                                 if (ConfigurationManager.TryGetSetting(OldMongoDbConnectionStringKey, out applicationKey) && !string.IsNullOrEmpty(applicationKey))
      84             :                                         return applicationKey;
      85             :                                 throw new MissingApplicationSettingForConnectionStringException(MongoDbConnectionStringKey);
      86             :                         }
      87             :                         finally
      88             :                         {
      89             :                                 Logger.LogDebug("Getting MongoDB connection string... Done", "MongoDataStoreConnectionStringFactory\\GetMongoConnectionString");
      90             :                         }
      91             :                 }
      92             : 
      93             :                 /// <summary>
      94             :                 /// Get the name of database on the MongoDB server.
      95             :                 /// </summary>
      96           1 :                 public string GetMongoDatabaseName()
      97             :                 {
      98             :                         string databaseName = ConfigurationManager.GetSetting(MongoDbDatabaseNameKey) ?? ConfigurationManager.GetSetting(OldMongoDbDatabaseNameKey);
      99             :                         if (string.IsNullOrEmpty(databaseName))
     100             :                                 throw new MissingApplicationSettingException(MongoDbDatabaseNameKey);
     101             :                         return databaseName;
     102             :                 }
     103             :         }
     104             : }

Generated by: LCOV version 1.10