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