Documentation Coverage Report
Current view: top level - Azure/Cqrs.Azure.BlobStorage/Events - TableStorageEventStoreConnectionStringFactory.cs Hit Total Coverage
Version: 2.2 Artefacts: 5 5 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.Collections.Generic;
      11             : using System.Linq;
      12             : using Cqrs.Configuration;
      13             : using cdmdotnet.Logging;
      14             : using Cqrs.Events;
      15             : 
      16             : namespace Cqrs.Azure.BlobStorage.Events
      17             : {
      18             :         /// <summary>
      19             :         /// A factory for getting connection strings and container names for <see cref="IEventStore{TAuthenticationToken}"/> access.
      20             :         /// This factory supports reading and writing from separate storage accounts. Specifically you can have as many different storage accounts as you want to configure when writing.
      21             :         /// This allows for manual mirroring of data while reading from the fastest/closest location possible.
      22             :         /// </summary>
      23             :         public class TableStorageEventStoreConnectionStringFactory : ITableStorageStoreConnectionStringFactory
      24           1 :         {
      25             :                 /// <summary>
      26             :                 /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the connection string of the readable storage account if using a separate storage account for reads and writes.
      27             :                 /// </summary>
      28             :                 public static string TableStorageReadableEventStoreConnectionStringKey = "Cqrs.Azure.TableStorage.EventStore.Read.ConnectionStringName";
      29             : 
      30             :                 /// <summary>
      31             :                 /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the connection string of the writeable storage account if using a separate storage account for reads and writes.
      32             :                 /// This value gets appended with a ".1", ".2" etc allowing you to write to as many different locations as possible.
      33             :                 /// </summary>
      34             :                 public static string TableStorageWritableEventStoreConnectionStringKey = "Cqrs.Azure.TableStorage.EventStore.Write.ConnectionStringName";
      35             : 
      36             :                 /// <summary>
      37             :                 /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the connection string if using a single storage account for both reads and writes.
      38             :                 /// </summary>
      39             :                 public static string TableStorageEventStoreConnectionStringKey = "Cqrs.Azure.TableStorage.EventStore.ConnectionStringName";
      40             : 
      41             :                 /// <summary>
      42             :                 /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the base name of the container used.
      43             :                 /// </summary>
      44             :                 public static string TableStorageBaseContainerNameKey = "Cqrs.Azure.TableStorage.EventStore.BaseContainerName";
      45             : 
      46             :                 /// <summary>
      47             :                 /// Gets or sets the <see cref="IConfigurationManager"/>.
      48             :                 /// </summary>
      49             :                 protected IConfigurationManager ConfigurationManager { get; private set; }
      50             : 
      51             :                 /// <summary>
      52             :                 /// Gets or sets the <see cref="ILogger"/>.
      53             :                 /// </summary>
      54             :                 protected ILogger Logger { get; private set; }
      55             : 
      56             :                 /// <summary>
      57             :                 /// Instantiates a new instance of <see cref="TableStorageEventStoreConnectionStringFactory"/>.
      58             :                 /// </summary>
      59           1 :                 public TableStorageEventStoreConnectionStringFactory(IConfigurationManager configurationManager, ILogger logger)
      60             :                 {
      61             :                         ConfigurationManager = configurationManager;
      62             :                         Logger = logger;
      63             :                 }
      64             : 
      65             :                 /// <summary>
      66             :                 /// Gets all writeable connection strings. If using a single storage account, then <see cref="TableStorageEventStoreConnectionStringKey"/> will most likely be returned.
      67             :                 /// If a value for <see cref="TableStorageWritableEventStoreConnectionStringKey"/> is found, it will append ".1", ".2" etc returning any additionally found connection string values in <see cref="ConfigurationManager"/>.
      68             :                 /// </summary>
      69           1 :                 public virtual IEnumerable<string> GetWritableConnectionStrings()
      70             :                 {
      71             :                         Logger.LogDebug("Getting table storage writeable connection strings", "TableStorageEventStoreConnectionStringFactory\\GetWritableConnectionStrings");
      72             :                         try
      73             :                         {
      74             :                                 var collection = new List<string> ();
      75             : 
      76             :                                 string tableStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(TableStorageWritableEventStoreConnectionStringKey);
      77             :                                 if (string.IsNullOrWhiteSpace(tableStorageWritableEventStoreConnectionString))
      78             :                                 {
      79             :                                         Logger.LogDebug(string.Format("No application setting named '{0}' in the configuration file.", TableStorageWritableEventStoreConnectionStringKey), "TableStorageEventStoreConnectionStringFactory\\GetWritableConnectionStrings");
      80             :                                         tableStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(TableStorageEventStoreConnectionStringKey);
      81             :                                 }
      82             : 
      83             :                                 int writeIndex = 1;
      84             :                                 while (!string.IsNullOrWhiteSpace(tableStorageWritableEventStoreConnectionString))
      85             :                                 {
      86             :                                         collection.Add(tableStorageWritableEventStoreConnectionString);
      87             : 
      88             :                                         tableStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(string.Format("{0}.{1}", TableStorageWritableEventStoreConnectionStringKey, writeIndex));
      89             : 
      90             :                                         writeIndex++;
      91             :                                 }
      92             : 
      93             :                                 if (!collection.Any())
      94             :                                         throw new NullReferenceException();
      95             : 
      96             :                                 return collection;
      97             :                         }
      98             :                         catch (NullReferenceException exception)
      99             :                         {
     100             :                                 throw new NullReferenceException(string.Format("No application settings named '{0}' was found in the configuration file with the cloud storage connection string.", TableStorageEventStoreConnectionStringKey), exception);
     101             :                         }
     102             :                         finally
     103             :                         {
     104             :                                 Logger.LogDebug("Getting table storage writeable connection string... Done", "TableStorageEventStoreConnectionStringFactory\\GetWritableConnectionStrings");
     105             :                         }
     106             :                 }
     107             : 
     108             :                 /// <summary>
     109             :                 /// Gets the readable connection string. If using a single storage account, then <see cref="TableStorageEventStoreConnectionStringKey"/> will most likely be returned.
     110             :                 /// If a value for <see cref="TableStorageReadableEventStoreConnectionStringKey"/> is found, that will be returned instead.
     111             :                 /// </summary>
     112           1 :                 public virtual string GetReadableConnectionString()
     113             :                 {
     114             :                         Logger.LogDebug("Getting table storage readable connection strings", "TableStorageEventStoreConnectionStringFactory\\GetReadableConnectionStrings");
     115             :                         try
     116             :                         {
     117             :                                 string tableStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(TableStorageReadableEventStoreConnectionStringKey);
     118             :                                 if (string.IsNullOrWhiteSpace(tableStorageWritableEventStoreConnectionString))
     119             :                                 {
     120             :                                         Logger.LogDebug(string.Format("No application setting named '{0}' in the configuration file.", TableStorageReadableEventStoreConnectionStringKey), "TableStorageEventStoreConnectionStringFactory\\GetReadableConnectionStrings");
     121             :                                         tableStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(TableStorageEventStoreConnectionStringKey);
     122             :                                 }
     123             : 
     124             :                                 if (string.IsNullOrWhiteSpace(tableStorageWritableEventStoreConnectionString))
     125             :                                         throw new NullReferenceException();
     126             : 
     127             :                                 return tableStorageWritableEventStoreConnectionString;
     128             :                         }
     129             :                         catch (NullReferenceException exception)
     130             :                         {
     131             :                                 throw new NullReferenceException(string.Format("No application settings named '{0}' was found in the configuration file with the cloud storage connection string.", TableStorageEventStoreConnectionStringKey), exception);
     132             :                         }
     133             :                         finally
     134             :                         {
     135             :                                 Logger.LogDebug("Getting table storage readable connection string... Done", "TableStorageEventStoreConnectionStringFactory\\GetReadableConnectionStrings");
     136             :                         }
     137             :                 }
     138             : 
     139             :                 /// <summary>
     140             :                 /// Returns the name of the base contain to be used.
     141             :                 /// This will be the value from <see cref="ConfigurationManager"/> keyed <see cref="TableStorageBaseContainerNameKey"/>.
     142             :                 /// </summary>
     143           1 :                 public virtual string GetBaseContainerName()
     144             :                 {
     145             :                         Logger.LogDebug("Getting table storage base container name", "TableStorageEventStoreConnectionStringFactory\\GetBaseContainerName");
     146             :                         try
     147             :                         {
     148             :                                 string result = ConfigurationManager.GetSetting(TableStorageBaseContainerNameKey);
     149             : 
     150             :                                 if (string.IsNullOrWhiteSpace(result))
     151             :                                         throw new NullReferenceException();
     152             : 
     153             :                                 return result;
     154             :                         }
     155             :                         catch (NullReferenceException exception)
     156             :                         {
     157             :                                 throw new NullReferenceException(string.Format("No application setting named '{0}' in the configuration file.", TableStorageBaseContainerNameKey), exception);
     158             :                         }
     159             :                         finally
     160             :                         {
     161             :                                 Logger.LogDebug("Getting table storage base container name... Done", "TableStorageEventStoreConnectionStringFactory\\GetBaseContainerName");
     162             :                         }
     163             :                 }
     164             :         }
     165             : }

Generated by: LCOV version 1.10