Documentation Coverage Report
Current view: top level - Cqrs.MongoDB/Factories - MongoDbDataStoreConnectionStringFactory.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.DataStores;
      14             : using Cqrs.Exceptions;
      15             : 
      16             : namespace Cqrs.MongoDB.Factories
      17             : {
      18             :         /// <summary>
      19             :         /// A factory for getting connection strings and database names for <see cref="IDataStore{TData}"/> access.
      20             :         /// </summary>
      21             :         public class MongoDbDataStoreConnectionStringFactory : IMongoDbDataStoreConnectionStringFactory
      22           1 :         {
      23             :                 /// <summary>
      24             :                 /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the name of the connection string of the MongoDB server.
      25             :                 /// </summary>
      26             :                 public static string MongoDbConnectionStringKey = "Cqrs.MongoDb.DataStore.ConnectionStringName";
      27             : 
      28             :                 /// <summary>
      29             :                 /// Backwards compatibility with version 1.
      30             :                 /// </summary>
      31             :                 public static string OldMongoDbConnectionStringKey = "CqrsMongoDb";
      32             : 
      33             :                 /// <summary>
      34             :                 /// Backwards compatibility with version 1.
      35             :                 /// </summary>
      36             :                 public static string OldMongoDbDatabaseNameKey = "CqrsMongoDbDatabaseName";
      37             : 
      38             :                 /// <summary>
      39             :                 /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the name of the database.
      40             :                 /// </summary>
      41             :                 public static string MongoDbDatabaseNameKey = "Cqrs.MongoDb.DataStore.DatabaseName";
      42             : 
      43             :                 /// <summary>
      44             :                 /// Gets or sets the <see cref="IConfigurationManager"/>.
      45             :                 /// </summary>
      46             :                 protected IConfigurationManager ConfigurationManager { get; private set; }
      47             : 
      48             :                 /// <summary>
      49             :                 /// Gets or sets the <see cref="ILogger"/>.
      50             :                 /// </summary>
      51             :                 protected ILogger Logger { get; private set; }
      52             : 
      53             :                 /// <summary>
      54             :                 /// Instantiates a new instance of <see cref="MongoDbDataStoreConnectionStringFactory"/>.
      55             :                 /// </summary>
      56           1 :                 public MongoDbDataStoreConnectionStringFactory(IConfigurationManager configurationManager, ILogger logger)
      57             :                 {
      58             :                         ConfigurationManager = configurationManager;
      59             :                         Logger = logger;
      60             :                 }
      61             : 
      62             :                 /// <summary>
      63             :                 /// Gets the current connection string.
      64             :                 /// </summary>
      65           1 :                 public string GetDataStoreConnectionString()
      66             :                 {
      67             :                         Logger.LogDebug("Getting MongoDB connection string", "MongoDbDataStoreConnectionStringFactory\\GetDataStoreConnectionString");
      68             :                         try
      69             :                         {
      70             :                                 string applicationKey;
      71             :                                 if (ConfigurationManager.TryGetSetting(MongoDbConnectionStringKey, out applicationKey) && !string.IsNullOrEmpty(applicationKey))
      72             :                                 {
      73             :                                         try
      74             :                                         {
      75             :                                                 ConnectionStringSettings connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[applicationKey];
      76             :                                                 if (connectionString != null)
      77             :                                                         return connectionString.ConnectionString;
      78             :                                         }
      79             :                                         catch (Exception exception)
      80             :                                         {
      81             :                                                 throw new MissingConnectionStringException(applicationKey, exception);
      82             :                                         }
      83             :                                 }
      84             :                                 if (ConfigurationManager.TryGetSetting(OldMongoDbConnectionStringKey, out applicationKey) && !string.IsNullOrEmpty(applicationKey))
      85             :                                         return applicationKey;
      86             :                                 throw new MissingApplicationSettingForConnectionStringException(MongoDbConnectionStringKey);
      87             :                         }
      88             :                         finally
      89             :                         {
      90             :                                 Logger.LogDebug("Getting MongoDB connection string... Done", "MongoDbDataStoreConnectionStringFactory\\GetDataStoreConnectionString");
      91             :                         }
      92             :                 }
      93             : 
      94             :                 /// <summary>
      95             :                 /// Gets the current database name.
      96             :                 /// </summary>
      97           1 :                 public string GetDataStoreDatabaseName()
      98             :                 {
      99             :                         string databaseName = ConfigurationManager.GetSetting(MongoDbDatabaseNameKey) ?? ConfigurationManager.GetSetting(OldMongoDbDatabaseNameKey);
     100             :                         if (string.IsNullOrEmpty(databaseName))
     101             :                                 throw new MissingApplicationSettingException(MongoDbDatabaseNameKey);
     102             :                         return databaseName;
     103             :                 }
     104             :         }
     105             : }

Generated by: LCOV version 1.10