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 cdmdotnet.Logging;
11 : using Cqrs.Configuration;
12 : using Cqrs.Events;
13 : using Microsoft.Azure.Documents.Client;
14 :
15 : namespace Cqrs.Azure.DocumentDb.Events
16 : {
17 : /// <summary>
18 : /// A factory for getting connections and database names for <see cref="IEventStore{TAuthenticationToken}"/> access.
19 : /// </summary>
20 : public class AzureDocumentDbEventStoreConnectionStringFactory
21 : : IAzureDocumentDbEventStoreConnectionStringFactory
22 1 : {
23 : /// <summary>
24 : /// Gets or sets the <see cref="ILogger"/>.
25 : /// </summary>
26 : protected ILogger Logger { get; private set; }
27 :
28 : /// <summary>
29 : /// Gets or sets the <see cref="IConfigurationManager"/>.
30 : /// </summary>
31 : protected IConfigurationManager ConfigurationManager { get; private set; }
32 :
33 : /// <summary>
34 : /// Instantiates a new instance of <see cref="AzureDocumentDbEventStoreConnectionStringFactory"/>.
35 : /// </summary>
36 1 : public AzureDocumentDbEventStoreConnectionStringFactory(ILogger logger, IConfigurationManager configurationManager)
37 : {
38 : Logger = logger;
39 : ConfigurationManager = configurationManager;
40 : }
41 :
42 : /// <summary>
43 : /// Gets the current <see cref="DocumentClient"/>.
44 : /// </summary>
45 1 : public virtual DocumentClient GetEventStoreConnectionClient()
46 : {
47 : Logger.LogDebug("Getting Azure document client", "AzureDocumentDbEventStoreConnectionStringFactory\\GetEventStoreConnectionClient");
48 : try
49 : {
50 : return new DocumentClient(GetEventStoreConnectionUrl(), GetEventStoreConnectionAuthorisationKey());
51 : }
52 : finally
53 : {
54 : Logger.LogDebug("Getting Azure document client... Done", "AzureDocumentDbEventStoreConnectionStringFactory\\GetEventStoreConnectionClient");
55 : }
56 : }
57 :
58 : /// <summary>
59 : /// Gets the current database name.
60 : /// </summary>
61 1 : public virtual string GetEventStoreConnectionDatabaseName()
62 : {
63 : return ConfigurationManager.GetSetting("Cqrs.EventStore.Azure.DocumentDb.DatabaseName") ?? ConfigurationManager.GetSetting("Cqrs.EventStore.Azure.DocumentDb.LogStreamName") ?? "CqrsEventStore";
64 : }
65 :
66 : /// <summary>
67 : /// Gets the current collection name.
68 : /// </summary>
69 1 : public string GetEventStoreConnectionCollectionName()
70 : {
71 : return ConfigurationManager.GetSetting("Cqrs.EventStore.Azure.DocumentDb.CollectionName") ?? "CqrsEventStore";
72 : }
73 :
74 : /// <summary>
75 : /// Gets the current connection <see cref="Uri"/> from the <see cref="ConfigurationManager"/>.
76 : /// </summary>
77 1 : protected virtual Uri GetEventStoreConnectionUrl()
78 : {
79 : return new Uri(ConfigurationManager.GetSetting("Cqrs.EventStore.Azure.DocumentDb.Url"));
80 : }
81 :
82 : /// <summary>
83 : /// Gets the current connection authorisation key from the <see cref="ConfigurationManager"/>.
84 : /// </summary>
85 1 : protected virtual string GetEventStoreConnectionAuthorisationKey()
86 : {
87 : return ConfigurationManager.GetSetting("Cqrs.EventStore.Azure.DocumentDb.AuthorisationKey");
88 : }
89 : }
90 : }
|