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.Linq;
10 : using cdmdotnet.Logging;
11 : using Cqrs.DataStores;
12 : using Microsoft.Azure.Documents;
13 : using Microsoft.Azure.Documents.Client;
14 : using Microsoft.Azure.Documents.Linq;
15 :
16 : namespace Cqrs.Azure.DocumentDb.Factories
17 : {
18 : /// <summary>
19 : /// A factory for obtaining <see cref="IDataStore{TData}"/> collections from Azure DocumentDB
20 : /// </summary>
21 : public class AzureDocumentDbDataStoreFactory
22 1 : {
23 : /// <summary>
24 : /// Gets or sets the <see cref="IAzureDocumentDbDataStoreConnectionStringFactory"/>.
25 : /// </summary>
26 : protected IAzureDocumentDbDataStoreConnectionStringFactory AzureDocumentDbDataStoreConnectionStringFactory { get; private set; }
27 :
28 : /// <summary>
29 : /// Gets or sets the <see cref="IAzureDocumentDbHelper"/>.
30 : /// </summary>
31 : protected IAzureDocumentDbHelper AzureDocumentDbHelper { get; private set; }
32 :
33 : /// <summary>
34 : /// Gets or sets the <see cref="ILogger"/>.
35 : /// </summary>
36 : protected ILogger Logger { get; private set; }
37 :
38 : /// <summary>
39 : /// Instantiates a new instance of <see cref="AzureDocumentDbDataStoreFactory"/>.
40 : /// </summary>
41 1 : public AzureDocumentDbDataStoreFactory(IAzureDocumentDbDataStoreConnectionStringFactory azureDocumentDbDataStoreConnectionStringFactory, IAzureDocumentDbHelper azureDocumentDbHelper, ILogger logger)
42 : {
43 : AzureDocumentDbDataStoreConnectionStringFactory = azureDocumentDbDataStoreConnectionStringFactory;
44 : AzureDocumentDbHelper = azureDocumentDbHelper;
45 : Logger = logger;
46 : }
47 :
48 : /// <summary>
49 : /// Get a <see cref="DocumentClient"/> from the <see cref="AzureDocumentDbDataStoreConnectionStringFactory"/>.
50 : /// </summary>
51 1 : protected virtual DocumentClient GetClient()
52 : {
53 : DocumentClient client = AzureDocumentDbDataStoreConnectionStringFactory.GetAzureDocumentDbConnectionClient();
54 :
55 : return client;
56 : }
57 :
58 : /// <summary>
59 : /// Get a <see cref="DocumentCollection"/> from the <see cref="AzureDocumentDbDataStoreConnectionStringFactory"/>.
60 : /// </summary>
61 1 : protected virtual DocumentCollection GetCollection<TEntity>(DocumentClient client, Database database)
62 : {
63 : string collectionName = string.Format(AzureDocumentDbDataStoreConnectionStringFactory.UseSingleCollectionForAllDataStores() ? "{0}" : "{0}_{1}", AzureDocumentDbDataStoreConnectionStringFactory.GetAzureDocumentDbCollectionName(), typeof(TEntity).FullName);
64 : DocumentCollection collection = AzureDocumentDbHelper.CreateOrReadCollection(client, database, collectionName).Result;
65 :
66 : return collection;
67 : }
68 :
69 : /// <summary>
70 : /// Get a blank <see cref="IOrderedQueryable{TEntity}"/>.
71 : /// </summary>
72 1 : protected virtual IOrderedQueryable<TEntity> GetQuery<TEntity>(DocumentClient client, DocumentCollection collection)
73 : {
74 : Logger.LogDebug("Getting Azure query", "AzureDocumentDbDataStoreFactory\\GetQuery");
75 : try
76 : {
77 : IOrderedQueryable<TEntity> query = client.CreateDocumentQuery<TEntity>(collection.SelfLink);
78 :
79 : return query;
80 : }
81 : finally
82 : {
83 : Logger.LogDebug("Getting Azure query... Done", "AzureDocumentDbDataStoreFactory\\GetQuery");
84 : }
85 : }
86 :
87 : /// <summary>
88 : /// Get the <see cref="Database"/> from the <see cref="AzureDocumentDbDataStoreConnectionStringFactory"/>.
89 : /// </summary>
90 1 : protected virtual Database GetDatabase(DocumentClient client)
91 : {
92 : return AzureDocumentDbHelper.CreateOrReadDatabase(client, AzureDocumentDbDataStoreConnectionStringFactory.GetAzureDocumentDbDatabaseName()).Result;
93 : }
94 : }
95 : }
|