forked from grandnode/grandnode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMongoDBRepository.cs
More file actions
347 lines (300 loc) · 9.91 KB
/
Copy pathMongoDBRepository.cs
File metadata and controls
347 lines (300 loc) · 9.91 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace Grand.Core.Data
{
/// <summary>
/// MongoDB repository
/// </summary>
public partial class MongoDBRepository<T> : IRepository<T> where T : BaseEntity
{
#region Fields
/// <summary>
/// Gets the collection
/// </summary>
protected IMongoCollection<T> _collection;
public IMongoCollection<T> Collection
{
get
{
return _collection;
}
}
/// <summary>
/// Mongo Database
/// </summary>
protected IMongoDatabase _database;
public IMongoDatabase Database
{
get
{
return _database;
}
}
#endregion
#region Ctor
/// <summary>
/// Ctor
/// </summary>
public MongoDBRepository()
{
string connectionString = DataSettingsHelper.ConnectionString();
if (!string.IsNullOrEmpty(connectionString))
{
var client = new MongoClient(connectionString);
var databaseName = new MongoUrl(connectionString).DatabaseName;
_database = client.GetDatabase(databaseName);
_collection = _database.GetCollection<T>(typeof(T).Name);
}
}
public MongoDBRepository(string connectionString)
{
var client = new MongoClient(connectionString);
var databaseName = new MongoUrl(connectionString).DatabaseName;
_database = client.GetDatabase(databaseName);
_collection = _database.GetCollection<T>(typeof(T).Name);
}
public MongoDBRepository(IMongoClient client)
{
string connectionString = DataSettingsHelper.ConnectionString();
var databaseName = new MongoUrl(connectionString).DatabaseName;
_database = client.GetDatabase(databaseName);
_collection = _database.GetCollection<T>(typeof(T).Name);
}
public MongoDBRepository(IMongoDatabase database)
{
_database = database;
_collection = _database.GetCollection<T>(typeof(T).Name);
}
#endregion
#region Methods
/// <summary>
/// Get entity by identifier
/// </summary>
/// <param name="id">Identifier</param>
/// <returns>Entity</returns>
public virtual T GetById(string id)
{
return this._collection.Find(e => e.Id == id).FirstOrDefault();
}
/// <summary>
/// Get entity by identifier async
/// </summary>
/// <param name="id">Identifier</param>
/// <returns>Entity</returns>
public virtual Task<T> GetByIdAsync(string id)
{
return this._collection.Find(e => e.Id == id).FirstOrDefaultAsync();
}
/// <summary>
/// Insert entity
/// </summary>
/// <param name="entity">Entity</param>
public virtual T Insert(T entity)
{
this._collection.InsertOne(entity);
return entity;
}
/// <summary>
/// Async Insert entity
/// </summary>
/// <param name="entity">Entity</param>
public virtual async Task<T> InsertAsync(T entity)
{
await this._collection.InsertOneAsync(entity);
return entity;
}
/// <summary>
/// Insert entities
/// </summary>
/// <param name="entities">Entities</param>
public virtual void Insert(IEnumerable<T> entities)
{
this._collection.InsertMany(entities);
}
/// <summary>
/// Async Insert entities
/// </summary>
/// <param name="entities">Entities</param>
public virtual async Task<IEnumerable<T>> InsertAsync(IEnumerable<T> entities)
{
await this._collection.InsertManyAsync(entities);
return entities;
}
/// <summary>
/// Update entity
/// </summary>
/// <param name="entity">Entity</param>
public virtual T Update(T entity)
{
this._collection.ReplaceOne(x=>x.Id == entity.Id, entity, new UpdateOptions() { IsUpsert = false });
return entity;
}
/// <summary>
/// Async Update entity
/// </summary>
/// <param name="entity">Entity</param>
public virtual async Task<T> UpdateAsync(T entity)
{
await this._collection.ReplaceOneAsync(x => x.Id == entity.Id, entity, new UpdateOptions() { IsUpsert = false });
return entity;
}
/// <summary>
/// Update entities
/// </summary>
/// <param name="entities">Entities</param>
public virtual void Update(IEnumerable<T> entities)
{
foreach (T entity in entities)
{
Update(entity);
}
}
/// <summary>
/// Async Update entities
/// </summary>
/// <param name="entities">Entities</param>
public virtual async Task<IEnumerable<T>> UpdateAsync(IEnumerable<T> entities)
{
foreach (T entity in entities)
{
await UpdateAsync(entity);
}
return entities;
}
/// <summary>
/// Delete entity
/// </summary>
/// <param name="entity">Entity</param>
public virtual void Delete(T entity)
{
this._collection.FindOneAndDelete(e => e.Id == entity.Id);
}
/// <summary>
/// Async Delete entity
/// </summary>
/// <param name="entity">Entity</param>
public virtual async Task<T> DeleteAsync(T entity)
{
await this._collection.DeleteOneAsync(e=>e.Id == entity.Id);
return entity;
}
/// <summary>
/// Delete entities
/// </summary>
/// <param name="entities">Entities</param>
public virtual void Delete(IEnumerable<T> entities)
{
foreach (T entity in entities)
{
this._collection.FindOneAndDeleteAsync(e => e.Id == entity.Id);
}
}
/// <summary>
/// Async Delete entities
/// </summary>
/// <param name="entities">Entities</param>
public virtual async Task<IEnumerable<T>> DeleteAsync(IEnumerable<T> entities)
{
foreach (T entity in entities)
{
await DeleteAsync(entity);
}
return entities;
}
#endregion
#region Methods
/// <summary>
/// Determines whether a list contains any elements
/// </summary>
/// <returns></returns>
public virtual bool Any()
{
return this._collection.AsQueryable().Any();
}
/// <summary>
/// Determines whether any element of a list satisfies a condition.
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public virtual bool Any(Expression<Func<T, bool>> where)
{
return this._collection.Find(where).Any();
}
/// <summary>
/// Async determines whether a list contains any elements
/// </summary>
/// <returns></returns>
public virtual async Task<bool> AnyAsync()
{
return await this._collection.AsQueryable().AnyAsync();
}
/// <summary>
/// Async determines whether any element of a list satisfies a condition.
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public virtual async Task<bool> AnyAsync(Expression<Func<T, bool>> where)
{
return await this._collection.Find(where).AnyAsync();
}
/// <summary>
/// Returns the number of elements in the specified sequence.
/// </summary>
/// <returns></returns>
public virtual long Count()
{
return this._collection.CountDocuments(new BsonDocument());
}
/// <summary>
/// Returns the number of elements in the specified sequence that satisfies a condition.
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public virtual long Count(Expression<Func<T, bool>> where)
{
return this._collection.CountDocuments(where);
}
/// <summary>
/// Async returns the number of elements in the specified sequence
/// </summary>
/// <returns></returns>
public virtual async Task<long> CountAsync()
{
return await this._collection.CountDocumentsAsync(new BsonDocument());
}
/// <summary>
/// Async returns the number of elements in the specified sequence that satisfies a condition.
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public virtual async Task<long> CountAsync(Expression<Func<T, bool>> where)
{
return await this._collection.CountDocumentsAsync(where);
}
#endregion
#region Properties
/// <summary>
/// Gets a table
/// </summary>
public virtual IMongoQueryable<T> Table
{
get { return this._collection.AsQueryable(); }
}
/// <summary>
/// Get collection by filter definitions
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public virtual IList<T> FindByFilterDefinition(FilterDefinition<T> query)
{
return this._collection.Find(query).ToList();
}
#endregion
}
}