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