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.Linq;
10 : using cdmdotnet.Logging;
11 : using Microsoft.Azure.Documents;
12 : using Microsoft.Azure.Documents.Client;
13 : using Microsoft.Azure.Documents.Linq;
14 :
15 : namespace Cqrs.Azure.DocumentDb.Factories
16 : {
17 : /// <summary>
18 : /// A factory for obtaining DataStore collections from Azure DocumentDB
19 : /// </summary>
20 : public class AzureDocumentDbDataStoreFactory
21 1 : {
22 : protected IAzureDocumentDbDataStoreConnectionStringFactory AzureDocumentDbDataStoreConnectionStringFactory { get; private set; }
23 :
24 : protected IAzureDocumentDbHelper AzureDocumentDbHelper { get; private set; }
25 :
26 : protected ILogger Logger { get; private set; }
27 :
28 0 : public AzureDocumentDbDataStoreFactory(IAzureDocumentDbDataStoreConnectionStringFactory azureDocumentDbDataStoreConnectionStringFactory, IAzureDocumentDbHelper azureDocumentDbHelper, ILogger logger)
29 : {
30 : AzureDocumentDbDataStoreConnectionStringFactory = azureDocumentDbDataStoreConnectionStringFactory;
31 : AzureDocumentDbHelper = azureDocumentDbHelper;
32 : Logger = logger;
33 : }
34 :
35 0 : protected virtual DocumentClient GetClient()
36 : {
37 : DocumentClient client = AzureDocumentDbDataStoreConnectionStringFactory.GetAzureDocumentDbConnectionClient();
38 :
39 : return client;
40 : }
41 :
42 0 : protected virtual DocumentCollection GetCollection<TEntity>(DocumentClient client, Database database)
43 : {
44 : string collectionName = string.Format(AzureDocumentDbDataStoreConnectionStringFactory.UseOneCollectionPerDataStore() ? "{0}" : "{0}_{1}", AzureDocumentDbDataStoreConnectionStringFactory.GetAzureDocumentDbCollectionName(), typeof(TEntity).FullName);
45 : DocumentCollection collection = AzureDocumentDbHelper.CreateOrReadCollection(client, database, collectionName).Result;
46 :
47 : return collection;
48 : }
49 :
50 0 : protected virtual IOrderedQueryable<TEntity> GetQuery<TEntity>(DocumentClient client, DocumentCollection collection)
51 : {
52 : Logger.LogDebug("Getting Azure query", "AzureDocumentDbDataStoreFactory\\GetQuery");
53 : try
54 : {
55 : IOrderedQueryable<TEntity> query = client.CreateDocumentQuery<TEntity>(collection.SelfLink);
56 :
57 : return query;
58 : }
59 : finally
60 : {
61 : Logger.LogDebug("Getting Azure query... Done", "AzureDocumentDbDataStoreFactory\\GetQuery");
62 : }
63 : }
64 :
65 0 : protected virtual Database GetDatabase(DocumentClient client)
66 : {
67 : return AzureDocumentDbHelper.CreateOrReadDatabase(client, AzureDocumentDbDataStoreConnectionStringFactory.GetAzureDocumentDbDatabaseName()).Result;
68 : }
69 : }
70 : }
|