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

Generated by: LCOV version 1.12