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.DataStores;
13 : using Cqrs.Entities;
14 : using Microsoft.Azure.Documents.Client;
15 :
16 : namespace Cqrs.Azure.DocumentDb.Factories
17 : {
18 : /// <summary>
19 : /// A factory for getting connections and database names for <see cref="IDataStore{TAuthenticationToken}"/> access.
20 : /// </summary>
21 : public class AzureDocumentDbDataStoreConnectionStringFactory : IAzureDocumentDbDataStoreConnectionStringFactory
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="AzureDocumentDbDataStoreConnectionStringFactory"/>.
35 : /// </summary>
36 1 : public AzureDocumentDbDataStoreConnectionStringFactory(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 GetAzureDocumentDbConnectionClient()
46 : {
47 : Logger.LogDebug("Getting Azure document client", "AzureDocumentDbDataStoreConnectionStringFactory\\GetAzureDocumentDbConnectionClient");
48 : try
49 : {
50 : return new DocumentClient(GetAzureDocumentDbConnectionUrl(), GetAzureDocumentDbAuthorisationKey());
51 : }
52 : finally
53 : {
54 : Logger.LogDebug("Getting Azure document client... Done", "AzureDocumentDbDataStoreConnectionStringFactory\\GetAzureDocumentDbConnectionClient");
55 : }
56 : }
57 :
58 : /// <summary>
59 : /// Gets the current database name.
60 : /// </summary>
61 1 : public virtual string GetAzureDocumentDbDatabaseName()
62 : {
63 : return ConfigurationManager.GetSetting("Cqrs.Azure.DocumentDb.DatabaseName") ?? "CqrsStore";
64 : }
65 :
66 : /// <summary>
67 : /// Gets the current collection name.
68 : /// </summary>
69 1 : public virtual string GetAzureDocumentDbCollectionName()
70 : {
71 : return ConfigurationManager.GetSetting("Cqrs.Azure.DocumentDb.CollectionName") ?? "CqrsDataStore";
72 : }
73 :
74 : /// <summary>
75 : /// Indicates if a different collection should be used per <see cref="IEntity"/>/<see cref="IDataStore{TData}"/> or a single collection used for all instances of <see cref="IDataStore{TData}"/> and <see cref="IDataStore{TData}"/>.
76 : /// Setting this to true can become expensive as each <see cref="IEntity"/> will have it's own collection. Check the relevant SDK/pricing models.
77 : /// </summary>
78 1 : public virtual bool UseSingleCollectionForAllDataStores()
79 : {
80 : bool value;
81 : if (!bool.TryParse(ConfigurationManager.GetSetting("Cqrs.Azure.DocumentDb.UseSingleCollectionForAllDataStores"), out value))
82 : value = true;
83 : return value;
84 : }
85 :
86 : /// <summary>
87 : /// Gets the current connection <see cref="Uri"/> from the <see cref="ConfigurationManager"/>.
88 : /// </summary>
89 1 : protected virtual Uri GetAzureDocumentDbConnectionUrl()
90 : {
91 : return new Uri(ConfigurationManager.GetSetting("Cqrs.Azure.DocumentDb.Url"));
92 : }
93 :
94 : /// <summary>
95 : /// Gets the current connection authorisation key from the <see cref="ConfigurationManager"/>.
96 : /// </summary>
97 1 : protected virtual string GetAzureDocumentDbAuthorisationKey()
98 : {
99 : return ConfigurationManager.GetSetting("Cqrs.Azure.DocumentDb.AuthorisationKey");
100 : }
101 : }
102 : }
|