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 BlobStorageEventStoreConnectionStringFactory : IBlobStorageStoreConnectionStringFactory
18 0 : {
19 : public static string BlobStorageReadableEventStoreConnectionStringKey = "Cqrs.Azure.BlobStorage.EventStore.Read.ConnectionStringName";
20 :
21 : public static string BlobStorageWritableEventStoreConnectionStringKey = "Cqrs.Azure.BlobStorage.EventStore.Write.ConnectionStringName";
22 :
23 : public static string BlobStorageEventStoreConnectionStringKey = "Cqrs.Azure.BlobStorage.EventStore.ConnectionStringName";
24 :
25 : public static string BlobStorageBaseContainerNameKey = "Cqrs.Azure.BlobStorage.EventStore.BaseContainerName";
26 :
27 : protected IConfigurationManager ConfigurationManager { get; private set; }
28 :
29 : protected ILogger Logger { get; private set; }
30 :
31 0 : public BlobStorageEventStoreConnectionStringFactory(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 blob storage writable connection strings", "BlobStorageEventStoreConnectionStringFactory\\GetWritableConnectionStrings");
40 : try
41 : {
42 : var collection = new List<string> ();
43 :
44 : string blobStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(BlobStorageWritableEventStoreConnectionStringKey);
45 : if (string.IsNullOrWhiteSpace(blobStorageWritableEventStoreConnectionString))
46 : {
47 : Logger.LogDebug(string.Format("No application setting named '{0}' in the configuration file.", BlobStorageWritableEventStoreConnectionStringKey), "BlobStorageEventStoreConnectionStringFactory\\GetWritableConnectionStrings");
48 : blobStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(BlobStorageEventStoreConnectionStringKey);
49 : }
50 :
51 : int writeIndex = 1;
52 : while (!string.IsNullOrWhiteSpace(blobStorageWritableEventStoreConnectionString))
53 : {
54 : collection.Add(blobStorageWritableEventStoreConnectionString);
55 :
56 : blobStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(string.Format("{0}.{1}", BlobStorageWritableEventStoreConnectionStringKey, 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.", BlobStorageEventStoreConnectionStringKey), exception);
69 : }
70 : finally
71 : {
72 : Logger.LogDebug("Getting blob storage writable connection string... Done", "BlobStorageEventStoreConnectionStringFactory\\GetWritableConnectionStrings");
73 : }
74 : }
75 :
76 0 : public virtual string GetReadableConnectionString()
77 : {
78 : Logger.LogDebug("Getting blob storage readable connection strings", "BlobStorageEventStoreConnectionStringFactory\\GetReadableConnectionStrings");
79 : try
80 : {
81 : string blobStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(BlobStorageReadableEventStoreConnectionStringKey);
82 : if (string.IsNullOrWhiteSpace(blobStorageWritableEventStoreConnectionString))
83 : {
84 : Logger.LogDebug(string.Format("No application setting named '{0}' in the configuration file.", BlobStorageReadableEventStoreConnectionStringKey), "BlobStorageEventStoreConnectionStringFactory\\GetReadableConnectionStrings");
85 : blobStorageWritableEventStoreConnectionString = ConfigurationManager.GetSetting(BlobStorageEventStoreConnectionStringKey);
86 : }
87 :
88 : if (string.IsNullOrWhiteSpace(blobStorageWritableEventStoreConnectionString))
89 : throw new NullReferenceException();
90 :
91 : return blobStorageWritableEventStoreConnectionString;
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.", BlobStorageEventStoreConnectionStringKey), exception);
96 : }
97 : finally
98 : {
99 : Logger.LogDebug("Getting blob storage readable connection string... Done", "BlobStorageEventStoreConnectionStringFactory\\GetReadableConnectionStrings");
100 : }
101 : }
102 :
103 0 : public virtual string GetBaseContainerName()
104 : {
105 : Logger.LogDebug("Getting blob storage base container name", "BlobStorageEventStoreConnectionStringFactory\\GetBaseContainerName");
106 : try
107 : {
108 : string result = ConfigurationManager.GetSetting(BlobStorageBaseContainerNameKey);
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.", BlobStorageBaseContainerNameKey), exception);
118 : }
119 : finally
120 : {
121 : Logger.LogDebug("Getting blob storage base container name... Done", "BlobStorageEventStoreConnectionStringFactory\\GetBaseContainerName");
122 : }
123 : }
124 : }
125 : }
|