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 Cqrs.Configuration;
13 : using cdmdotnet.Logging;
14 :
15 : namespace Cqrs.Azure.BlobStorage.Events
16 : {
17 : public class TableStorageEventStoreConnectionStringFactory : ITableStorageStoreConnectionStringFactory
18 0 : {
19 : public static string TableStorageReadableEventStoreConnectionStringKey = "Cqrs.Azure.TableStorage.EventStore.Read.ConnectionStringName";
20 :
21 : public static string TableStorageWritableEventStoreConnectionStringKey = "Cqrs.Azure.TableStorage.EventStore.Write.ConnectionStringName";
22 :
23 : public static string TableStorageEventStoreConnectionStringKey = "Cqrs.Azure.TableStorage.EventStore.ConnectionStringName";
24 :
25 : public static string TableStorageBaseContainerNameKey = "Cqrs.Azure.TableStorage.EventStore.BaseContainerName";
26 :
27 : protected IConfigurationManager ConfigurationManager { get; private set; }
28 :
29 : protected ILogger Logger { get; private set; }
30 :
31 0 : public TableStorageEventStoreConnectionStringFactory(IConfigurationManager configurationManager, ILogger logger)
32 : {
33 : ConfigurationManager = configurationManager;
34 : Logger = logger;
35 : }
36 :
37 0 : public virtual IEnumerable<string> GetWritableConnectionStrings()
38 : {
39 : Logger.LogDebug("Getting table storage writable connection strings", "TableStorageEventStoreConnectionStringFactory\\GetWritableConnectionStrings");
40 : try
41 : {
42 : var collection = new List<string> ();
43 :
44 : string tableStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(TableStorageWritableEventStoreConnectionStringKey);
45 : if (string.IsNullOrWhiteSpace(tableStorageWritableEventStoreConnectionString))
46 : {
47 : Logger.LogDebug(string.Format("No application setting named '{0}' in the configuration file.", TableStorageWritableEventStoreConnectionStringKey), "TableStorageEventStoreConnectionStringFactory\\GetWritableConnectionStrings");
48 : tableStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(TableStorageEventStoreConnectionStringKey);
49 : }
50 :
51 : int writeIndex = 1;
52 : while (!string.IsNullOrWhiteSpace(tableStorageWritableEventStoreConnectionString))
53 : {
54 : collection.Add(tableStorageWritableEventStoreConnectionString);
55 :
56 : tableStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(string.Format("{0}.{1}", TableStorageWritableEventStoreConnectionStringKey, writeIndex));
57 :
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 settings named '{0}' was found in the configuration file with the cloud storage connection string.", TableStorageEventStoreConnectionStringKey), exception);
69 : }
70 : finally
71 : {
72 : Logger.LogDebug("Getting table storage writable connection string... Done", "TableStorageEventStoreConnectionStringFactory\\GetWritableConnectionStrings");
73 : }
74 : }
75 :
76 0 : public virtual string GetReadableConnectionString()
77 : {
78 : Logger.LogDebug("Getting table storage readable connection strings", "TableStorageEventStoreConnectionStringFactory\\GetReadableConnectionStrings");
79 : try
80 : {
81 : string tableStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(TableStorageReadableEventStoreConnectionStringKey);
82 : if (string.IsNullOrWhiteSpace(tableStorageWritableEventStoreConnectionString))
83 : {
84 : Logger.LogDebug(string.Format("No application setting named '{0}' in the configuration file.", TableStorageReadableEventStoreConnectionStringKey), "TableStorageEventStoreConnectionStringFactory\\GetReadableConnectionStrings");
85 : tableStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(TableStorageEventStoreConnectionStringKey);
86 : }
87 :
88 : if (string.IsNullOrWhiteSpace(tableStorageWritableEventStoreConnectionString))
89 : throw new NullReferenceException();
90 :
91 : return tableStorageWritableEventStoreConnectionString;
92 : }
93 : catch (NullReferenceException exception)
94 : {
95 : throw new NullReferenceException(string.Format("No application settings named '{0}' was found in the configuration file with the cloud storage connection string.", TableStorageEventStoreConnectionStringKey), exception);
96 : }
97 : finally
98 : {
99 : Logger.LogDebug("Getting table storage readable connection string... Done", "TableStorageEventStoreConnectionStringFactory\\GetReadableConnectionStrings");
100 : }
101 : }
102 :
103 0 : public virtual string GetBaseContainerName()
104 : {
105 : Logger.LogDebug("Getting table storage base container name", "TableStorageEventStoreConnectionStringFactory\\GetBaseContainerName");
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", "TableStorageEventStoreConnectionStringFactory\\GetBaseContainerName");
122 : }
123 : }
124 : }
125 : }
|