LCOV - code coverage report
Current view: top level - Azure/Cqrs.Azure.BlobStorage/DataStores - TableStorageDataStoreConnectionStringFactory.cs Hit Total Coverage
Test: doc-coverage.info Lines: 1 7 14.3 %
Date: 2017-07-26

          Line data    Source code
       1             : #region Copyright
       2             : // // -----------------------------------------------------------------------
       3             : // // <copyright company="cdmdotnet Limited">
       4             : // //   Copyright cdmdotnet Limited. All rights reserved.
       5             : // // </copyright>
       6             : // // -----------------------------------------------------------------------
       7             : #endregion
       8             : 
       9             : using System;
      10             : using System.Collections.Generic;
      11             : using System.Linq;
      12             : using System.Text;
      13             : using Cqrs.Configuration;
      14             : using cdmdotnet.Logging;
      15             : 
      16             : namespace Cqrs.Azure.BlobStorage.DataStores
      17             : {
      18             :         public class TableStorageDataStoreConnectionStringFactory : ITableStorageDataStoreConnectionStringFactory
      19           0 :         {
      20             :                 public static string TableStorageReadableDataStoreConnectionStringKey = "Cqrs.Azure.TableStorage.DataStore.Read.ConnectionStringName";
      21             : 
      22             :                 public static string TableStorageWritableDataStoreConnectionStringKey = "Cqrs.Azure.TableStorage.DataStore.Write.ConnectionStringName";
      23             : 
      24             :                 public static string TableStorageDataStoreConnectionStringKey = "Cqrs.Azure.TableStorage.DataStore.ConnectionStringName";
      25             : 
      26             :                 public static string TableStorageBaseContainerNameKey = "Cqrs.Azure.TableStorage.DataStore.BaseContainerName";
      27             : 
      28             :                 protected IConfigurationManager ConfigurationManager { get; private set; }
      29             : 
      30             :                 protected ILogger Logger { get; private set; }
      31             : 
      32           0 :                 public TableStorageDataStoreConnectionStringFactory(IConfigurationManager configurationManager, ILogger logger)
      33             :                 {
      34             :                         ConfigurationManager = configurationManager;
      35             :                         Logger = logger;
      36             :                 }
      37             : 
      38           0 :                 public virtual IEnumerable<string> GetWritableConnectionStrings()
      39             :                 {
      40             :                         Logger.LogDebug("Getting table storage writeable connection strings", "TableStorageDataStoreConnectionStringFactory\\GetWritableConnectionStrings");
      41             :                         try
      42             :                         {
      43             :                                 var collection = new List<string> ();
      44             : 
      45             :                                 string blobStorageWritableDataStoreConnectionString = ConfigurationManager.GetSetting(TableStorageWritableDataStoreConnectionStringKey);
      46             :                                 if (blobStorageWritableDataStoreConnectionString == null)
      47             :                                 {
      48             :                                         Logger.LogDebug(string.Format("No application setting named '{0}' was found in the configuration file with the cloud storage connection string.", TableStorageWritableDataStoreConnectionStringKey), "TableStorageDataStoreConnectionStringFactory\\GetWritableConnectionStrings");
      49             :                                         blobStorageWritableDataStoreConnectionString = ConfigurationManager.GetSetting(TableStorageDataStoreConnectionStringKey);
      50             :                                 }
      51             : 
      52             :                                 int writeIndex = 1;
      53             :                                 while (!string.IsNullOrWhiteSpace(blobStorageWritableDataStoreConnectionString))
      54             :                                 {
      55             :                                         collection.Add(blobStorageWritableDataStoreConnectionString);
      56             : 
      57             :                                         blobStorageWritableDataStoreConnectionString = ConfigurationManager.GetSetting(string.Format("{0}.{1}", TableStorageWritableDataStoreConnectionStringKey, writeIndex));
      58             :                                         writeIndex++;
      59             :                                 }
      60             : 
      61             :                                 if (!collection.Any())
      62             :                                         throw new NullReferenceException();
      63             : 
      64             :                                 return collection;
      65             :                         }
      66             :                         catch (NullReferenceException exception)
      67             :                         {
      68             :                                 throw new NullReferenceException(string.Format("No application setting named '{0}' was found in the configuration file with the cloud storage connection string.", TableStorageDataStoreConnectionStringKey), exception);
      69             :                         }
      70             :                         finally
      71             :                         {
      72             :                                 Logger.LogDebug("Getting table storage writeable connection string... Done", "TableStorageDataStoreConnectionStringFactory\\GetWritableConnectionStrings");
      73             :                         }
      74             :                 }
      75             : 
      76           0 :                 public virtual string GetReadableConnectionString()
      77             :                 {
      78             :                         Logger.LogDebug("Getting table storage readable connection strings", "TableStorageDataStoreConnectionStringFactory\\GetReadableConnectionStrings");
      79             :                         try
      80             :                         {
      81             :                                 string blobStorageWritableDataStoreConnectionString = ConfigurationManager.GetSetting(TableStorageReadableDataStoreConnectionStringKey);
      82             :                                 if (blobStorageWritableDataStoreConnectionString == null)
      83             :                                 {
      84             :                                         Logger.LogDebug(string.Format("No application setting named '{0}' was found in the configuration file with the cloud storage connection string.", TableStorageReadableDataStoreConnectionStringKey), "TableStorageDataStoreConnectionStringFactory\\GetReadableConnectionStrings");
      85             :                                         blobStorageWritableDataStoreConnectionString = ConfigurationManager.GetSetting(TableStorageDataStoreConnectionStringKey);
      86             :                                 }
      87             : 
      88             :                                 if (string.IsNullOrWhiteSpace(blobStorageWritableDataStoreConnectionString))
      89             :                                         throw new NullReferenceException();
      90             : 
      91             :                                 return blobStorageWritableDataStoreConnectionString;
      92             :                         }
      93             :                         catch (NullReferenceException exception)
      94             :                         {
      95             :                                 throw new NullReferenceException(string.Format("No application setting named '{0}' was found in the configuration file with the cloud storage connection string.", TableStorageDataStoreConnectionStringKey), exception);
      96             :                         }
      97             :                         finally
      98             :                         {
      99             :                                 Logger.LogDebug("Getting table storage readable connection string... Done", "TableStorageDataStoreConnectionStringFactory\\GetReadableConnectionStrings");
     100             :                         }
     101             :                 }
     102             : 
     103           0 :                 public string GetBaseContainerName()
     104             :                 {
     105             :                         Logger.LogDebug("Getting table storage base container name", "TableStorageDataStoreConnectionStringFactory\\GetContainerName");
     106             :                         try
     107             :                         {
     108             :                                 string result = ConfigurationManager.GetSetting(TableStorageBaseContainerNameKey);
     109             : 
     110             :                                 if (string.IsNullOrWhiteSpace(result))
     111             :                                         throw new NullReferenceException();
     112             : 
     113             :                                 return result;
     114             :                         }
     115             :                         catch (NullReferenceException exception)
     116             :                         {
     117             :                                 throw new NullReferenceException(string.Format("No application setting named '{0}' in the configuration file.", TableStorageBaseContainerNameKey), exception);
     118             :                         }
     119             :                         finally
     120             :                         {
     121             :                                 Logger.LogDebug("Getting table storage base container name... Done", "TableStorageDataStoreConnectionStringFactory\\GetContainerName");
     122             :                         }
     123             :                 }
     124             : 
     125           0 :                 public virtual string GetContainerName()
     126             :                 {
     127             :                         return GetBaseContainerName();
     128             :                 }
     129             : 
     130             :                 readonly char[] _alphanumericCharacters =
     131             :                 {
     132             :                         '0','1','2','3','4','5','6','7','8','9',
     133             :                         'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
     134             :                         'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
     135             :                 };
     136             :                 /// <remarks>https://blogs.msdn.microsoft.com/jmstall/2014/06/12/azure-storage-naming-rules/</remarks>
     137           1 :                 public virtual string GetTableName<TData>()
     138             :                 {
     139             :                         Type type = typeof (TData);
     140             :                         string fullTableName = type.AssemblyQualifiedName ?? "Entities";
     141             :                         StringBuilder sb;
     142             : 
     143             :                         string name = fullTableName;
     144             :                         int index1 = name.IndexOf(",", StringComparison.InvariantCultureIgnoreCase);
     145             :                         int index2 = -1;
     146             :                         if (index1 > -1)
     147             :                                 index2 = name.IndexOf(",", index1 + 1, StringComparison.InvariantCultureIgnoreCase);
     148             :                         if (index2 > -1)
     149             :                         {
     150             :                                 name = name.Substring(0, index2);
     151             :                                 string[] nameParts = name.Split(',');
     152             :                                 if (nameParts.Length == 2)
     153             :                                 {
     154             :                                         if (nameParts[0].StartsWith(nameParts[1].Trim()))
     155             :                                         {
     156             :                                                 name = name.Substring(0, index1);
     157             :                                                 sb = new StringBuilder();
     158             :                                                 foreach (var c in name.Where(c => _alphanumericCharacters.Contains(c)))
     159             :                                                         sb.Append(c);
     160             : 
     161             :                                                 name = sb.ToString();
     162             :                                                 if (name.Length > 36)
     163             :                                                         name = name.Substring(name.Length - 36);
     164             : 
     165             :                                                 return name;
     166             :                                         }
     167             :                                 }
     168             :                         }
     169             :                         else if (index1 > -1)
     170             :                                 name = name.Substring(0, index1);
     171             : 
     172             :                         sb = new StringBuilder();
     173             :                         foreach (var c in name.Where(c => _alphanumericCharacters.Contains(c)))
     174             :                                 sb.Append(c);
     175             : 
     176             :                         name = sb.ToString();
     177             :                         return name.Substring(0, 36);
     178             :                 }
     179             :         }
     180             : }

Generated by: LCOV version 1.10