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 cdmdotnet.StateManagement;
10 : using Microsoft.Azure.Documents;
11 : using Microsoft.Azure.Documents.Client;
12 :
13 : namespace Cqrs.Azure.DocumentDb
14 : {
15 : /// <summary>
16 : /// A cache manager for DocumentDB clients, databases and collections that is thread based.
17 : /// </summary>
18 : public class ThreadedAzureDocumentDbConnectionCache : IAzureDocumentDbConnectionCache
19 1 : {
20 : /// <summary>
21 : /// Gets the <see cref="IContextItemCollection">cache</see> used.
22 : /// </summary>
23 : protected IContextItemCollection Cache { get; private set; }
24 :
25 : /// <summary>
26 : /// Instantiates a new instance of <see cref="ThreadedAzureDocumentDbConnectionCache"/>.
27 : /// </summary>
28 1 : public ThreadedAzureDocumentDbConnectionCache(IContextItemCollectionFactory factory)
29 : {
30 : Cache = factory.GetCurrentContext();
31 : }
32 :
33 : /// <summary>
34 : /// Gets the <see cref="DocumentClient"/>.
35 : /// </summary>
36 : /// <param name="key">The name of the <see cref="DocumentClient"/> to get.</param>
37 : /// <param name="client">If the <see cref="DocumentClient"/> is found, it is returned here; otherwise null is returned. This parameter is passed uninitialized.</param>
38 : /// <returns>true if the <see cref="DocumentClient"/> is found; otherwise, false.</returns>
39 1 : public bool TryGetClient(string key, out DocumentClient client)
40 : {
41 : try
42 : {
43 : var results = Cache.GetData<DocumentClient>(key);
44 : if (results != null)
45 : {
46 : client = results;
47 : return true;
48 : }
49 : }
50 : catch { /* */ }
51 : client = null;
52 : return false;
53 : }
54 :
55 : /// <summary>
56 : /// Sets the provided <paramref name="client"/>.
57 : /// </summary>
58 : /// <param name="key">The name of the <see cref="DocumentClient"/> to get.</param>
59 : /// <param name="client">The <see cref="DocumentClient"/> to set.</param>
60 1 : public void SetClient(string key, DocumentClient client)
61 : {
62 : Cache.SetData(key, client);
63 : }
64 :
65 : /// <summary>
66 : /// Gets the <see cref="Database"/>.
67 : /// </summary>
68 : /// <param name="key">The name of the <see cref="Database"/> to get.</param>
69 : /// <param name="database">If the <see cref="Database"/> is found, it is returned here; otherwise null is returned. This parameter is passed uninitialized.</param>
70 : /// <returns>true if the <see cref="Database"/> is found; otherwise, false.</returns>
71 1 : public bool TryGetDatabase(string key, out Database database)
72 : {
73 : try
74 : {
75 : var results = Cache.GetData<Database>(key);
76 : if (results != null)
77 : {
78 : database = results;
79 : return true;
80 : }
81 : }
82 : catch { /* */ }
83 : database = null;
84 : return false;
85 : }
86 :
87 : /// <summary>
88 : /// Sets the provided <paramref name="database"/>.
89 : /// </summary>
90 : /// <param name="key">The name of the <see cref="Database"/> to get.</param>
91 : /// <param name="database">The <see cref="Database"/> to set.</param>
92 1 : public void SetDatabase(string key, Database database)
93 : {
94 : Cache.SetData(key, database);
95 : }
96 :
97 : /// <summary>
98 : /// Gets the <see cref="DocumentCollection"/>.
99 : /// </summary>
100 : /// <param name="key">The name of the <see cref="DocumentCollection"/> to get.</param>
101 : /// <param name="documentCollection">If the <see cref="DocumentCollection"/> is found, it is returned here; otherwise null is returned. This parameter is passed uninitialized.</param>
102 : /// <returns>true if the <see cref="DocumentCollection"/> is found; otherwise, false.</returns>
103 1 : public bool TryGetDocumentCollection(string key, out DocumentCollection documentCollection)
104 : {
105 : try
106 : {
107 : var results = Cache.GetData<DocumentCollection>(key);
108 : if (results != null)
109 : {
110 : documentCollection = results;
111 : return true;
112 : }
113 : }
114 : catch { /* */ }
115 : documentCollection = null;
116 : return false;
117 : }
118 :
119 : /// <summary>
120 : /// Sets the provided <paramref name="documentCollection"/>.
121 : /// </summary>
122 : /// <param name="key">The name of the <see cref="DocumentCollection"/> to get.</param>
123 : /// <param name="documentCollection">The <see cref="DocumentCollection"/> to set.</param>
124 1 : public void SetDocumentCollection(string key, DocumentCollection documentCollection)
125 : {
126 : Cache.SetData(key, documentCollection);
127 : }
128 : }
129 : }
|