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.Configuration;
11 : using cdmdotnet.Logging;
12 : using Cqrs.Configuration;
13 : using Cqrs.Events;
14 : using Cqrs.Exceptions;
15 :
16 : namespace Cqrs.MongoDB.Events
17 : {
18 : /// <summary>
19 : /// A factory for getting connection strings and database names for <see cref="IEventStore{TAuthenticationToken}"/> access.
20 : /// </summary>
21 : public class MongoDbEventStoreConnectionStringFactory : IMongoDbEventStoreConnectionStringFactory
22 1 : {
23 : /// <summary>
24 : /// Backwards compatibility with version 1.
25 : /// </summary>
26 : public static string MongoDbConnectionStringKey = "CqrsMongoDbEventStore";
27 :
28 : /// <summary>
29 : /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the name of the connection string of the MongoDB server.
30 : /// </summary>
31 : public static string MongoDbConnectionNameApplicationKey = "Cqrs.MongoDb.EventStore.ConnectionStringName";
32 :
33 : /// <summary>
34 : /// Backwards compatibility with version 1.
35 : /// </summary>
36 : public static string MongoDbDatabaseNameKey = "CqrsMongoDbEventStoreDatabaseName";
37 :
38 : /// <summary>
39 : /// The name of the app setting in <see cref="IConfigurationManager"/> that will have the name of the database.
40 : /// </summary>
41 : public static string MongoDbDatabaseNameApplicationKey = "Cqrs.MongoDb.EventStore.DatabaseName";
42 :
43 : /// <summary>
44 : /// Gets or sets the <see cref="IConfigurationManager"/>.
45 : /// </summary>
46 : protected IConfigurationManager ConfigurationManager { get; private set; }
47 :
48 : /// <summary>
49 : /// Gets or sets the <see cref="ILogger"/>.
50 : /// </summary>
51 : protected ILogger Logger { get; private set; }
52 :
53 : /// <summary>
54 : /// Instantiates a new instance of <see cref="MongoDbEventStoreConnectionStringFactory"/>.
55 : /// </summary>
56 1 : public MongoDbEventStoreConnectionStringFactory(IConfigurationManager configurationManager, ILogger logger)
57 : {
58 : ConfigurationManager = configurationManager;
59 : Logger = logger;
60 : }
61 :
62 : #region Implementation of IMongoDbEventStoreConnectionStringFactory
63 :
64 : /// <summary>
65 : /// Gets the current connection string.
66 : /// </summary>
67 1 : public string GetEventStoreConnectionString()
68 : {
69 : Logger.LogDebug("Getting MongoDB connection string", "MongoDbEventStoreConnectionStringFactory\\GetEventStoreConnectionString");
70 :
71 : try
72 : {
73 : string applicationKey;
74 :
75 : if (!ConfigurationManager.TryGetSetting(MongoDbConnectionNameApplicationKey, out applicationKey) || string.IsNullOrEmpty(applicationKey))
76 : {
77 : Logger.LogDebug(string.Format("No application setting named '{0}' was found in the configuration file with the name of a connection string to look for.", MongoDbConnectionNameApplicationKey), "MongoDbEventStoreConnectionStringFactory\\GetEventStoreConnectionString");
78 :
79 : if (!ConfigurationManager.TryGetSetting(MongoDbConnectionStringKey, out applicationKey) || string.IsNullOrEmpty(applicationKey))
80 : {
81 : Logger.LogDebug(string.Format("No application setting named '{0}' was found in the configuration file with the name of a connection string to look for.", MongoDbConnectionStringKey), "MongoDbEventStoreConnectionStringFactory\\GetEventStoreConnectionString");
82 : throw new MissingApplicationSettingForConnectionStringException(MongoDbConnectionNameApplicationKey);
83 : }
84 : }
85 :
86 : ConnectionStringSettings connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[MongoDbConnectionStringKey];
87 : // If the connection string doesn't exist this value IS the connection string itself
88 : if (connectionString == null)
89 : return applicationKey;
90 :
91 : return connectionString.ConnectionString;
92 : }
93 : finally
94 : {
95 : Logger.LogDebug("Getting MongoDB connection string... Done", "MongoDbEventStoreConnectionStringFactory\\GetEventStoreConnectionString");
96 : }
97 : }
98 :
99 : /// <summary>
100 : /// Gets the current database name.
101 : /// </summary>
102 1 : public string GetEventStoreDatabaseName()
103 : {
104 : string applicationKey;
105 :
106 : if (!ConfigurationManager.TryGetSetting(MongoDbDatabaseNameApplicationKey, out applicationKey) || string.IsNullOrEmpty(applicationKey))
107 : {
108 : if (!ConfigurationManager.TryGetSetting(MongoDbDatabaseNameKey, out applicationKey) || string.IsNullOrEmpty(applicationKey))
109 : {
110 : throw new NullReferenceException(string.Format("No application setting named '{0}' was found in the configuration file with the name of the collection.", MongoDbDatabaseNameApplicationKey));
111 : }
112 : }
113 : return applicationKey;
114 : }
115 :
116 : #endregion
117 : }
118 : }
|