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: 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 BlobStorageEventStoreConnectionStringFactory : IBlobStorageStoreConnectionStringFactory
      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 BlobStorageReadableEventStoreConnectionStringKey = "Cqrs.Azure.BlobStorage.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 BlobStorageWritableEventStoreConnectionStringKey = "Cqrs.Azure.BlobStorage.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 BlobStorageEventStoreConnectionStringKey = "Cqrs.Azure.BlobStorage.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 BlobStorageBaseContainerNameKey = "Cqrs.Azure.BlobStorage.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="BlobStorageEventStoreConnectionStringFactory"/>.
      58             :                 /// </summary>
      59           1 :                 public BlobStorageEventStoreConnectionStringFactory(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="BlobStorageEventStoreConnectionStringKey"/> will most likely be returned.
      67             :                 /// 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"/>.
      68             :                 /// </summary>
      69           1 :                 public virtual IEnumerable<string> GetWritableConnectionStrings()
      70             :                 {
      71             :                         Logger.LogDebug("Getting blob storage writeable connection strings", "BlobStorageEventStoreConnectionStringFactory\\GetWritableConnectionStrings");
      72             :                         try
      73             :                         {
      74             :                                 var collection = new List<string> ();
      75             : 
      76             :                                 string blobStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(BlobStorageWritableEventStoreConnectionStringKey);
      77             :                                 if (string.IsNullOrWhiteSpace(blobStorageWritableEventStoreConnectionString))
      78             :                                 {
      79             :                                         Logger.LogDebug(string.Format("No application setting named '{0}' in the configuration file.", BlobStorageWritableEventStoreConnectionStringKey), "BlobStorageEventStoreConnectionStringFactory\\GetWritableConnectionStrings");
      80             :                                         blobStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(BlobStorageEventStoreConnectionStringKey);
      81             :                                 }
      82             : 
      83             :                                 int writeIndex = 1;
      84             :                                 while (!string.IsNullOrWhiteSpace(blobStorageWritableEventStoreConnectionString))
      85             :                                 {
      86             :                                         collection.Add(blobStorageWritableEventStoreConnectionString);
      87             : 
      88             :                                         blobStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(string.Format("{0}.{1}", BlobStorageWritableEventStoreConnectionStringKey, 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.", BlobStorageEventStoreConnectionStringKey), exception);
     101             :                         }
     102             :                         finally
     103             :                         {
     104             :                                 Logger.LogDebug("Getting blob storage writeable connection string... Done", "BlobStorageEventStoreConnectionStringFactory\\GetWritableConnectionStrings");
     105             :                         }
     106             :                 }
     107             : 
     108             :                 /// <summary>
     109             :                 /// Gets the readable connection string. If using a single storage account, then <see cref="BlobStorageEventStoreConnectionStringKey"/> will most likely be returned.
     110             :                 /// If a value for <see cref="BlobStorageReadableEventStoreConnectionStringKey"/> is found, that will be returned instead.
     111             :                 /// </summary>
     112           1 :                 public virtual string GetReadableConnectionString()
     113             :                 {
     114             :                         Logger.LogDebug("Getting blob storage readable connection strings", "BlobStorageEventStoreConnectionStringFactory\\GetReadableConnectionStrings");
     115             :                         try
     116             :                         {
     117             :                                 string blobStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(BlobStorageReadableEventStoreConnectionStringKey);
     118             :                                 if (string.IsNullOrWhiteSpace(blobStorageWritableEventStoreConnectionString))
     119             :                                 {
     120             :                                         Logger.LogDebug(string.Format("No application setting named '{0}' in the configuration file.", BlobStorageReadableEventStoreConnectionStringKey), "BlobStorageEventStoreConnectionStringFactory\\GetReadableConnectionStrings");
     121             :                                         blobStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(BlobStorageEventStoreConnectionStringKey);
     122             :                                 }
     123             : 
     124             :                                 if (string.IsNullOrWhiteSpace(blobStorageWritableEventStoreConnectionString))
     125             :                                         throw new NullReferenceException();
     126             : 
     127             :                                 return blobStorageWritableEventStoreConnectionString;
     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.", BlobStorageEventStoreConnectionStringKey), exception);
     132             :                         }
     133             :                         finally
     134             :                         {
     135             :                                 Logger.LogDebug("Getting blob storage readable connection string... Done", "BlobStorageEventStoreConnectionStringFactory\\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="BlobStorageBaseContainerNameKey"/>.
     142             :                 /// </summary>
     143           1 :                 public virtual string GetBaseContainerName()
     144             :                 {
     145             :                         Logger.LogDebug("Getting blob storage base container name", "BlobStorageEventStoreConnectionStringFactory\\GetBaseContainerName");
     146             :                         try
     147             :                         {
     148             :                                 string result = ConfigurationManager.GetSetting(BlobStorageBaseContainerNameKey);
     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.", BlobStorageBaseContainerNameKey), exception);
     158             :                         }
     159             :                         finally
     160             :                         {
     161             :                                 Logger.LogDebug("Getting blob storage base container name... Done", "BlobStorageEventStoreConnectionStringFactory\\GetBaseContainerName");
     162             :                         }
     163             :                 }
     164             :         }
     165             : }

Generated by: LCOV version 1.10