Documentation Coverage Report
Current view: top level - Cqrs.MongoDB/Events - MongoDbEventStoreConnectionStringFactory.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;
      10             : using System.Configuration;
      11             : using cdmdotnet.Logging;
      12             : using Cqrs.Configuration;
      13             : using Cqrs.Events;
      14             : using Cqrs.Exceptions;
      15             : 
      16             : namespace Cqrs.MongoDB.Events
      17             : {
      18             :         /// <summary>
      19             :         /// A factory for getting connection strings and database names for <see cref="IEventStore{TAuthenticationToken}"/> access.
      20             :         /// </summary>
      21             :         public class MongoDbEventStoreConnectionStringFactory
      22             :                 : IMongoDbEventStoreConnectionStringFactory
      23           1 :         {
      24             :                 /// <summary>
      25             :                 /// Backwards compatibility with version 1.
      26             :                 /// </summary>
      27             :                 public static string MongoDbConnectionStringKey = "CqrsMongoDbEventStore";
      28             : 
      29             :                 /// <summary>
      30             :                 /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the name of the connection string of the MongoDB server.
      31             :                 /// </summary>
      32             :                 public static string MongoDbConnectionNameApplicationKey = "Cqrs.MongoDb.EventStore.ConnectionStringName";
      33             : 
      34             :                 /// <summary>
      35             :                 /// Backwards compatibility with version 1.
      36             :                 /// </summary>
      37             :                 public static string MongoDbDatabaseNameKey = "CqrsMongoDbEventStoreDatabaseName";
      38             : 
      39             :                 /// <summary>
      40             :                 /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the name of the database.
      41             :                 /// </summary>
      42             :                 public static string MongoDbDatabaseNameApplicationKey = "Cqrs.MongoDb.EventStore.DatabaseName";
      43             : 
      44             :                 /// <summary>
      45             :                 /// Gets or sets the <see cref="IConfigurationManager"/>.
      46             :                 /// </summary>
      47             :                 protected IConfigurationManager ConfigurationManager { get; private set; }
      48             : 
      49             :                 /// <summary>
      50             :                 /// Gets or sets the <see cref="ILogger"/>.
      51             :                 /// </summary>
      52             :                 protected ILogger Logger { get; private set; }
      53             : 
      54             :                 /// <summary>
      55             :                 /// Instantiates a new instance of <see cref="MongoDbEventStoreConnectionStringFactory"/>.
      56             :                 /// </summary>
      57           1 :                 public MongoDbEventStoreConnectionStringFactory(IConfigurationManager configurationManager, ILogger logger)
      58             :                 {
      59             :                         ConfigurationManager = configurationManager;
      60             :                         Logger = logger;
      61             :                 }
      62             : 
      63             :                 #region Implementation of IMongoDbEventStoreConnectionStringFactory
      64             : 
      65             :                 /// <summary>
      66             :                 /// Gets the current connection string.
      67             :                 /// </summary>
      68           1 :                 public string GetEventStoreConnectionString()
      69             :                 {
      70             :                         Logger.LogDebug("Getting MongoDB connection string", "MongoDbEventStoreConnectionStringFactory\\GetEventStoreConnectionString");
      71             : 
      72             :                         try
      73             :                         {
      74             :                                 string applicationKey;
      75             : 
      76             :                                 if (!ConfigurationManager.TryGetSetting(MongoDbConnectionNameApplicationKey, out applicationKey) || string.IsNullOrEmpty(applicationKey))
      77             :                                 {
      78             :                                         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), "MongoDbEventStoreConnectionStringFactory\\GetEventStoreConnectionString");
      79             : 
      80             :                                         if (!ConfigurationManager.TryGetSetting(MongoDbConnectionStringKey, out applicationKey) || string.IsNullOrEmpty(applicationKey))
      81             :                                         {
      82             :                                                 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.", MongoDbConnectionStringKey), "MongoDbEventStoreConnectionStringFactory\\GetEventStoreConnectionString");
      83             :                                                 throw new MissingApplicationSettingForConnectionStringException(MongoDbConnectionNameApplicationKey);
      84             :                                         }
      85             :                                 }
      86             : 
      87             :                                 ConnectionStringSettings connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[applicationKey];
      88             :                                 // If the connection string doesn't exist this value IS the connection string itself
      89             :                                 if (connectionString == null)
      90             :                                         return applicationKey;
      91             : 
      92             :                                 return connectionString.ConnectionString;
      93             :                         }
      94             :                         finally
      95             :                         {
      96             :                                 Logger.LogDebug("Getting MongoDB connection string... Done", "MongoDbEventStoreConnectionStringFactory\\GetEventStoreConnectionString");
      97             :                         }
      98             :                 }
      99             : 
     100             :                 /// <summary>
     101             :                 /// Gets the current database name.
     102             :                 /// </summary>
     103           1 :                 public string GetEventStoreDatabaseName()
     104             :                 {
     105             :                         string applicationKey;
     106             : 
     107             :                         if (!ConfigurationManager.TryGetSetting(MongoDbDatabaseNameApplicationKey, out applicationKey) || string.IsNullOrEmpty(applicationKey))
     108             :                         {
     109             :                                 if (!ConfigurationManager.TryGetSetting(MongoDbDatabaseNameKey, out applicationKey) || string.IsNullOrEmpty(applicationKey))
     110             :                                 {
     111             :                                         throw new NullReferenceException(string.Format("No application setting named '{0}' was found in the configuration file with the name of the collection.", MongoDbDatabaseNameApplicationKey));
     112             :                                 }
     113             :                         }
     114             :                         return applicationKey;
     115             :                 }
     116             : 
     117             :                 #endregion
     118             :         }
     119             : }

Generated by: LCOV version 1.12