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
186 lines (160 loc) · 5.06 KB
/
Copy pathMongoDBRepository.cs
File metadata and controls
186 lines (160 loc) · 5.06 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
using System.Collections.Generic;
using System.Linq;
using Grand.Core;
using Grand.Core.Data;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
namespace Grand.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(IMongoClient client, IMongoDatabase mongodatabase)
{
_database = mongodatabase;
_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>
/// Insert entity
/// </summary>
/// <param name="entity">Entity</param>
public virtual T Insert(T entity)
{
this._collection.InsertOne(entity);
return entity;
}
/// <summary>
/// Insert entities
/// </summary>
/// <param name="entities">Entities</param>
public virtual void Insert(IEnumerable<T> entities)
{
foreach (var entity in entities)
Insert(entity);
}
/// <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>
/// Update entities
/// </summary>
/// <param name="entities">Entities</param>
public virtual void Update(IEnumerable<T> entities)
{
foreach (T entity in entities)
{
Update(entity);
}
}
/// <summary>
/// Delete entity
/// </summary>
/// <param name="entity">Entity</param>
public virtual void Delete(T entity)
{
this._collection.FindOneAndDeleteAsync(e => e.Id == entity.Id);
}
/// <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);
}
}
#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
}
}