This repository was archived by the owner on Feb 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathMongoRepository.cs
More file actions
331 lines (295 loc) · 11.8 KB
/
MongoRepository.cs
File metadata and controls
331 lines (295 loc) · 11.8 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
namespace MongoRepository
{
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
/// <summary>
/// Deals with entities in MongoDb.
/// </summary>
/// <typeparam name="T">The type contained in the repository.</typeparam>
/// <typeparam name="TKey">The type used for the entity's Id.</typeparam>
public class MongoRepository<T, TKey> : IRepository<T, TKey>
where T : IEntity<TKey>
{
/// <summary>
/// MongoCollection field.
/// </summary>
protected internal MongoCollection<T> collection;
/// <summary>
/// Initializes a new instance of the MongoRepository class.
/// Uses the Default App/Web.Config connectionstrings to fetch the connectionString and Database name.
/// </summary>
/// <remarks>Default constructor defaults to "MongoServerSettings" key for connectionstring.</remarks>
public MongoRepository()
: this(Util<TKey>.GetDefaultConnectionString())
{
}
/// <summary>
/// Initializes a new instance of the MongoRepository class.
/// </summary>
/// <param name="connectionString">Connectionstring to use for connecting to MongoDB.</param>
public MongoRepository(string connectionString)
{
this.collection = Util<TKey>.GetCollectionFromConnectionString<T>(connectionString);
}
/// <summary>
/// Initializes a new instance of the MongoRepository class.
/// </summary>
/// <param name="connectionString">Connectionstring to use for connecting to MongoDB.</param>
/// <param name="collectionName">The name of the collection to use.</param>
public MongoRepository(string connectionString, string collectionName)
{
this.collection = Util<TKey>.GetCollectionFromConnectionString<T>(connectionString, collectionName);
}
/// <summary>
/// Initializes a new instance of the MongoRepository class.
/// </summary>
/// <param name="url">Url to use for connecting to MongoDB.</param>
public MongoRepository(MongoUrl url)
{
this.collection = Util<TKey>.GetCollectionFromUrl<T>(url);
}
/// <summary>
/// Initializes a new instance of the MongoRepository class.
/// </summary>
/// <param name="url">Url to use for connecting to MongoDB.</param>
/// <param name="collectionName">The name of the collection to use.</param>
public MongoRepository(MongoUrl url, string collectionName)
{
this.collection = Util<TKey>.GetCollectionFromUrl<T>(url, collectionName);
}
/// <summary>
/// Gets the Mongo collection (to perform advanced operations).
/// </summary>
/// <remarks>
/// One can argue that exposing this property (and with that, access to it's Database property for instance
/// (which is a "parent")) is not the responsibility of this class. Use of this property is highly discouraged;
/// for most purposes you can use the MongoRepositoryManager<T>
/// </remarks>
/// <value>The Mongo collection (to perform advanced operations).</value>
public MongoCollection<T> Collection
{
get { return this.collection; }
}
/// <summary>
/// Gets the name of the collection
/// </summary>
public string CollectionName
{
get { return this.collection.Name; }
}
/// <summary>
/// Returns the T by its given id.
/// </summary>
/// <param name="id">The Id of the entity to retrieve.</param>
/// <returns>The Entity T.</returns>
public virtual T GetById(TKey id)
{
if (typeof(T).IsSubclassOf(typeof(Entity)))
{
return this.GetById(new ObjectId(id as string));
}
return this.collection.FindOneByIdAs<T>(BsonValue.Create(id));
}
/// <summary>
/// Returns the T by its given id.
/// </summary>
/// <param name="id">The Id of the entity to retrieve.</param>
/// <returns>The Entity T.</returns>
public virtual T GetById(ObjectId id)
{
return this.collection.FindOneByIdAs<T>(id);
}
/// <summary>
/// Adds the new entity in the repository.
/// </summary>
/// <param name="entity">The entity T.</param>
/// <returns>The added entity including its new ObjectId.</returns>
public virtual T Add(T entity)
{
this.collection.Insert<T>(entity);
return entity;
}
/// <summary>
/// Adds the new entities in the repository.
/// </summary>
/// <param name="entities">The entities of type T.</param>
public virtual void Add(IEnumerable<T> entities)
{
this.collection.InsertBatch<T>(entities);
}
/// <summary>
/// Upserts an entity.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns>The updated entity.</returns>
public virtual T Update(T entity)
{
this.collection.Save<T>(entity);
return entity;
}
/// <summary>
/// Upserts the entities.
/// </summary>
/// <param name="entities">The entities to update.</param>
public virtual void Update(IEnumerable<T> entities)
{
foreach (T entity in entities)
{
this.collection.Save<T>(entity);
}
}
/// <summary>
/// Deletes an entity from the repository by its id.
/// </summary>
/// <param name="id">The entity's id.</param>
public virtual void Delete(TKey id)
{
if (typeof(T).IsSubclassOf(typeof(Entity)))
{
this.collection.Remove(Query.EQ("_id", new ObjectId(id as string)));
}
else
{
this.collection.Remove(Query.EQ("_id", BsonValue.Create(id)));
}
}
/// <summary>
/// Deletes an entity from the repository by its ObjectId.
/// </summary>
/// <param name="id">The ObjectId of the entity.</param>
public virtual void Delete(ObjectId id)
{
this.collection.Remove(Query.EQ("_id", id));
}
/// <summary>
/// Deletes the given entity.
/// </summary>
/// <param name="entity">The entity to delete.</param>
public virtual void Delete(T entity)
{
this.Delete(entity.Id);
}
/// <summary>
/// Deletes the entities matching the predicate.
/// </summary>
/// <param name="predicate">The expression.</param>
public virtual void Delete(Expression<Func<T, bool>> predicate)
{
foreach (T entity in this.collection.AsQueryable<T>().Where(predicate))
{
this.Delete(entity.Id);
}
}
/// <summary>
/// Deletes all entities in the repository.
/// </summary>
public virtual void DeleteAll()
{
this.collection.RemoveAll();
}
/// <summary>
/// Counts the total entities in the repository.
/// </summary>
/// <returns>Count of entities in the collection.</returns>
public virtual long Count()
{
return this.collection.Count();
}
/// <summary>
/// Checks if the entity exists for given predicate.
/// </summary>
/// <param name="predicate">The expression.</param>
/// <returns>True when an entity matching the predicate exists, false otherwise.</returns>
public virtual bool Exists(Expression<Func<T, bool>> predicate)
{
return this.collection.AsQueryable<T>().Any(predicate);
}
#region IQueryable<T>
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>An IEnumerator<T> object that can be used to iterate through the collection.</returns>
public virtual IEnumerator<T> GetEnumerator()
{
return this.collection.AsQueryable<T>().GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>An IEnumerator object that can be used to iterate through the collection.</returns>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.collection.AsQueryable<T>().GetEnumerator();
}
/// <summary>
/// Gets the type of the element(s) that are returned when the expression tree associated with this instance of IQueryable is executed.
/// </summary>
public virtual Type ElementType
{
get { return this.collection.AsQueryable<T>().ElementType; }
}
/// <summary>
/// Gets the expression tree that is associated with the instance of IQueryable.
/// </summary>
public virtual Expression Expression
{
get { return this.collection.AsQueryable<T>().Expression; }
}
/// <summary>
/// Gets the query provider that is associated with this data source.
/// </summary>
public virtual IQueryProvider Provider
{
get { return this.collection.AsQueryable<T>().Provider; }
}
#endregion
}
/// <summary>
/// Deals with entities in MongoDb.
/// </summary>
/// <typeparam name="T">The type contained in the repository.</typeparam>
/// <remarks>Entities are assumed to use strings for Id's.</remarks>
public class MongoRepository<T> : MongoRepository<T, string>, IRepository<T>
where T : IEntity<string>
{
/// <summary>
/// Initializes a new instance of the MongoRepository class.
/// Uses the Default App/Web.Config connectionstrings to fetch the connectionString and Database name.
/// </summary>
/// <remarks>Default constructor defaults to "MongoServerSettings" key for connectionstring.</remarks>
public MongoRepository()
: base() { }
/// <summary>
/// Initializes a new instance of the MongoRepository class.
/// </summary>
/// <param name="url">Url to use for connecting to MongoDB.</param>
public MongoRepository(MongoUrl url)
: base(url) { }
/// <summary>
/// Initializes a new instance of the MongoRepository class.
/// </summary>
/// <param name="url">Url to use for connecting to MongoDB.</param>
/// <param name="collectionName">The name of the collection to use.</param>
public MongoRepository(MongoUrl url, string collectionName)
: base(url, collectionName) { }
/// <summary>
/// Initializes a new instance of the MongoRepository class.
/// </summary>
/// <param name="connectionString">Connectionstring to use for connecting to MongoDB.</param>
public MongoRepository(string connectionString)
: base(connectionString) { }
/// <summary>
/// Initializes a new instance of the MongoRepository class.
/// </summary>
/// <param name="connectionString">Connectionstring to use for connecting to MongoDB.</param>
/// <param name="collectionName">The name of the collection to use.</param>
public MongoRepository(string connectionString, string collectionName)
: base(connectionString, collectionName) { }
}
}