Documentation Coverage Report
Current view: top level - Cqrs.MongoDB/Events - MongoDbSnapshotStoreConnectionStringFactory.cs Hit Total Coverage
Version: 2.2 Artefacts: 4 4 100.0 %
Date: 2018-08-07 15:04:50

          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.Configuration;
      10             : using cdmdotnet.Logging;
      11             : using Cqrs.Configuration;
      12             : using Cqrs.Exceptions;
      13             : 
      14             : namespace Cqrs.MongoDB.Events
      15             : {
      16             :         /// <summary>
      17             :         /// A factory for getting connection strings and database names for Snapshot Store access.
      18             :         /// </summary>
      19             :         public class MongoDbSnapshotStoreConnectionStringFactory
      20             :                 : IMongoDbSnapshotStoreConnectionStringFactory
      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 MongoDbConnectionNameApplicationKey = "Cqrs.MongoDb.SnapshotStore.ConnectionStringName";
      26             : 
      27             :                 /// <summary>
      28             :                 /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the name of the database.
      29             :                 /// </summary>
      30             :                 public static string MongoDbDatabaseNameApplicationKey = "Cqrs.MongoDb.SnapshotStore.DatabaseName";
      31             : 
      32             :                 /// <summary>
      33             :                 /// Gets or sets the <see cref="IConfigurationManager"/>.
      34             :                 /// </summary>
      35             :                 protected IConfigurationManager ConfigurationManager { get; private set; }
      36             : 
      37             :                 /// <summary>
      38             :                 /// Gets or sets the <see cref="ILogger"/>.
      39             :                 /// </summary>
      40             :                 protected ILogger Logger { get; private set; }
      41             : 
      42             :                 /// <summary>
      43             :                 /// Instantiates a new instance of <see cref="MongoDbSnapshotStoreConnectionStringFactory"/>.
      44             :                 /// </summary>
      45           1 :                 public MongoDbSnapshotStoreConnectionStringFactory(IConfigurationManager configurationManager, ILogger logger)
      46             :                 {
      47             :                         ConfigurationManager = configurationManager;
      48             :                         Logger = logger;
      49             :                 }
      50             : 
      51             :                 #region Implementation of IMongoDbSnapshotStoreConnectionStringFactory
      52             : 
      53             :                 /// <summary>
      54             :                 /// Gets the current connection string.
      55             :                 /// </summary>
      56           1 :                 public string GetSnapshotStoreConnectionString()
      57             :                 {
      58             :                         Logger.LogDebug("Getting MongoDB connection string", "MongoDbSnapshotStoreConnectionStringFactory\\GetSnapshotStoreConnectionString");
      59             : 
      60             :                         try
      61             :                         {
      62             :                                 string applicationKey;
      63             : 
      64             :                                 if (!ConfigurationManager.TryGetSetting(MongoDbConnectionNameApplicationKey, out applicationKey) || string.IsNullOrEmpty(applicationKey))
      65             :                                 {
      66             :                                         Logger.LogDebug(string.Format("No application setting named '{0}' was found in the configuration file with the name of a connection string to look for.", MongoDbConnectionNameApplicationKey), "MongoDbSnapshotStoreConnectionStringFactory\\GetSnapshotStoreConnectionString");
      67             :                                         throw new MissingApplicationSettingForConnectionStringException(MongoDbConnectionNameApplicationKey);
      68             :                                 }
      69             : 
      70             :                                 ConnectionStringSettings connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[applicationKey];
      71             :                                 // If the connection string doesn't exist this value IS the connection string itself
      72             :                                 if (connectionString == null)
      73             :                                         throw new MissingConnectionStringException(applicationKey);
      74             : 
      75             :                                 return connectionString.ConnectionString;
      76             :                         }
      77             :                         finally
      78             :                         {
      79             :                                 Logger.LogDebug("Getting MongoDB connection string... Done", "MongoDbSnapshotStoreConnectionStringFactory\\GetSnapshotStoreConnectionString");
      80             :                         }
      81             :                 }
      82             : 
      83             :                 /// <summary>
      84             :                 /// Gets the current database name.
      85             :                 /// </summary>
      86           1 :                 public string GetSnapshotStoreDatabaseName()
      87             :                 {
      88             :                         string applicationKey;
      89             : 
      90             :                         if (!ConfigurationManager.TryGetSetting(MongoDbDatabaseNameApplicationKey, out applicationKey) || string.IsNullOrEmpty(applicationKey))
      91             :                                 throw new MissingApplicationSettingException(MongoDbDatabaseNameApplicationKey);
      92             :                         return applicationKey;
      93             :                 }
      94             : 
      95             :                 #endregion
      96             :         }
      97             : }

Generated by: LCOV version 1.12