-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentDBRepository.cs
180 lines (154 loc) · 7.08 KB
/
DocumentDBRepository.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
namespace NET.DocumentDb.Repository
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace Azure.Documents.Db
{
using System.Configuration;
using System.Linq;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
public class DocumentDbRepository
{
public DocumentDbRepository(string collection)
{
this.CollectionId = collection;
}
public IEnumerable<T> GetItems<T>(Expression<Func<T, bool>> predicate)
{
return this.Client.CreateDocumentQuery<T>(this.Collection.DocumentsLink, new FeedOptions { EnableScanInQuery = true })
.Where(predicate)
.AsEnumerable();
}
public IEnumerable<T> GetItems<T>(Expression<Func<T, bool>> predicate, FeedOptions options)
{
return this.Client.CreateDocumentQuery<T>(this.Collection.DocumentsLink, options)
.Where(predicate)
.AsEnumerable();
}
/// <summary>
/// Get items by string expression query
/// </summary>
/// <typeparam name="T">Entity type to retorn</typeparam>
/// <param name="expression">Query expression to get entities result</param>
/// <returns>Entities returned by query expression</returns>
public IEnumerable<T> GetItems<T>(string expression)
{
return this.Client.CreateDocumentQuery<T>(this.Collection.DocumentsLink, expression).AsEnumerable();
}
/// <summary>
/// Get item by string expression query
/// </summary>
/// <typeparam name="T">Entity type to retorn</typeparam>
/// <param name="expression">Query expression to get first or default entity</param>
/// <returns>Entity returned by query expression</returns>
public T GetItem<T>(string expression)
{
return this.Client.CreateDocumentQuery<T>(this.Collection.DocumentsLink, expression)
.AsEnumerable()
.FirstOrDefault();
}
public T GetItem<T>(Expression<Func<T, bool>> predicate)
{
return this.Client.CreateDocumentQuery<T>(this.Collection.DocumentsLink)
.Where(predicate)
.AsEnumerable()
.FirstOrDefault();
}
public async Task CreateItemAsync<T>(T item)
{
await this.Client.CreateDocumentAsync(this.Collection.SelfLink, item);
}
public async Task UpdateItemAsync<T>(string id, T item)
{
var doc = this.GetDocument(id);
await this.Client.ReplaceDocumentAsync(doc.SelfLink, item);
}
//Use the Database if it exists, if not create a new Database
private Database ReadOrCreateDatabase()
{
var db = this.Client.CreateDatabaseQuery()
.Where(d => d.Id == this.DatabaseId)
.AsEnumerable()
.FirstOrDefault() ?? this.Client.CreateDatabaseAsync(new Database { Id = this.DatabaseId }).Result;
return db;
}
private Document GetDocument(string id)
{
return this.Client.CreateDocumentQuery(this.Collection.DocumentsLink)
.Where(d => d.Id == id)
.AsEnumerable()
.FirstOrDefault();
}
//Use the DocumentCollection if it exists, if not create a new Collection
private DocumentCollection ReadOrCreateCollection(string databaseLink)
{
var col = this.Client.CreateDocumentCollectionQuery(databaseLink)
.Where(c => c.Id == this.CollectionId)
.AsEnumerable()
.FirstOrDefault();
if (col != null)
{
return col;
}
var collectionSpec = new DocumentCollection { Id = this.CollectionId };
var requestOptions = new RequestOptions { OfferType = "S1" };
col = this.Client.CreateDocumentCollectionAsync(databaseLink, collectionSpec, requestOptions).Result;
col.IndexingPolicy.IncludedPaths.Add(
new IncludedPath
{
Path = "/*",
Indexes = new Collection<Index>
{
new RangeIndex(DataType.Number) { Precision = 7 }
}
});
return col;
}
//Expose the "database" value from configuration as a property for internal use
private string databaseId;
private string DatabaseId
{
get
{
if (string.IsNullOrEmpty(this.databaseId))
{
this.databaseId = ConfigurationManager.AppSettings["database"];
}
return this.databaseId;
}
}
//Expose the "collection" value from configuration as a property for internal use
private string CollectionId { get; }
//Use the ReadOrCreateDatabase function to get a reference to the database.
private Database database;
private Database Database => this.database ?? (this.database = this.ReadOrCreateDatabase());
//Use the ReadOrCreateCollection function to get a reference to the collection.
private DocumentCollection collection;
private DocumentCollection Collection => this.collection ?? (this.collection = this.ReadOrCreateCollection(this.Database.SelfLink));
//This property establishes a new connection to DocumentDB the first time it is used,
//and then reuses this instance for the duration of the application avoiding the
//overhead of instantiating a new instance of DocumentClient with each request
private DocumentClient client;
private DocumentClient Client
{
get
{
if (this.client != null)
{
return this.client;
}
var endpoint = ConfigurationManager.AppSettings["endpoint"];
var authKey = ConfigurationManager.AppSettings["authKey"];
var endpointUri = new Uri(endpoint);
this.client = new DocumentClient(endpointUri, authKey);
return this.client;
}
}
}
}
}