-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMongoEntityRepository.cs
More file actions
156 lines (118 loc) · 5.95 KB
/
MongoEntityRepository.cs
File metadata and controls
156 lines (118 loc) · 5.95 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
using MongoDB.Bson;
using MongoDB.Driver;
using System.Linq.Expressions;
using uBeac.Repositories.History;
namespace uBeac.Repositories.MongoDB;
public class MongoEntityRepository<TKey, TEntity, TContext> : IEntityRepository<TKey, TEntity>
where TKey : IEquatable<TKey>
where TEntity : IEntity<TKey>
where TContext : IMongoDBContext
{
protected readonly IMongoCollection<TEntity> Collection;
protected readonly IMongoCollection<BsonDocument> BsonCollection;
protected readonly IMongoDatabase MongoDatabase;
protected readonly TContext MongoDbContext;
protected readonly IApplicationContext ApplicationContext;
protected readonly IHistoryManager History;
public MongoEntityRepository(TContext mongoDbContext, IApplicationContext applicationContext, IHistoryManager history)
{
MongoDatabase = mongoDbContext.Database;
Collection = mongoDbContext.Database.GetCollection<TEntity>(GetCollectionName());
BsonCollection = mongoDbContext.Database.GetCollection<BsonDocument>(GetCollectionName());
MongoDbContext = mongoDbContext;
ApplicationContext = applicationContext;
History = history;
}
protected virtual string GetCollectionName()
{
return typeof(TEntity).Name;
}
public virtual async Task Delete(TKey id, string actionName, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var idFilter = Builders<TEntity>.Filter.Eq(doc => doc.Id, id);
var entity = await Collection.FindOneAndDeleteAsync(idFilter, null, cancellationToken);
await History.Write(entity, actionName, cancellationToken);
}
public virtual async Task Delete(TKey id, CancellationToken cancellationToken = default)
{
await Delete(id, nameof(Delete), cancellationToken);
}
public virtual async Task<IEnumerable<TEntity>> GetAll(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var filter = Builders<TEntity>.Filter.Empty;
var findResult = await Collection.FindAsync(filter, null, cancellationToken);
return await findResult.ToListAsync(cancellationToken);
}
public virtual async Task<TEntity> GetById(TKey id, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var idFilter = Builders<TEntity>.Filter.Eq(x => x.Id, id);
var findResult = await Collection.FindAsync(idFilter, null, cancellationToken);
return await findResult.SingleOrDefaultAsync(cancellationToken);
}
public virtual async Task<IEnumerable<TEntity>> GetByIds(IEnumerable<TKey> ids, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var idsFilter = Builders<TEntity>.Filter.In(x => x.Id, ids);
var findResult = await Collection.FindAsync(idsFilter, null, cancellationToken);
return await findResult.ToListAsync(cancellationToken);
}
public virtual async Task Create(TEntity entity, string actionName = null, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
// If the entity is extend from IAuditEntity, the audit properties (CreatedAt, CreatedBy, etc.) should be set
if (entity is IAuditEntity<TKey> audit) SetPropertiesOnCreate(audit, ApplicationContext);
await Collection.InsertOneAsync(entity, null, cancellationToken);
await History.Write(entity, actionName, cancellationToken);
}
public virtual async Task Create(TEntity entity, CancellationToken cancellationToken = default)
{
await Create(entity, nameof(Create), cancellationToken);
}
public virtual async Task Update(TEntity entity, string actionName, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
// If the entity is extend from IAuditEntity, the audit properties (LastUpdatedAt, LastUpdatedBy, etc.) should be set
if (entity is IAuditEntity<TKey> audit) SetPropertiesOnUpdate(audit, ApplicationContext);
var idFilter = Builders<TEntity>.Filter.Eq(x => x.Id, entity.Id);
await Collection.FindOneAndReplaceAsync(idFilter, entity, null, cancellationToken);
await History.Write(entity, actionName, cancellationToken);
}
public virtual async Task Update(TEntity entity, CancellationToken cancellationToken = default)
{
await Update(entity, nameof(Update), cancellationToken);
}
public virtual async Task<IEnumerable<TEntity>> Find(Expression<Func<TEntity, bool>> filter, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var findResult = await Collection.FindAsync(filter, cancellationToken: cancellationToken);
return await findResult.ToListAsync(cancellationToken);
}
public IQueryable<TEntity> AsQueryable() => Collection.AsQueryable();
private void SetPropertiesOnCreate(IAuditEntity<TKey> entity, IApplicationContext appContext)
{
var now = DateTime.Now;
var userName = appContext.UserName;
entity.CreatedAt = now;
entity.CreatedBy = userName;
entity.LastUpdatedAt = now;
entity.LastUpdatedBy = userName;
}
private void SetPropertiesOnUpdate(IAuditEntity<TKey> entity, IApplicationContext appContext)
{
var now = DateTime.Now;
var userName = appContext.UserName;
entity.LastUpdatedAt = now;
entity.LastUpdatedBy = userName;
}
}
public class MongoEntityRepository<TEntity, TContext> : MongoEntityRepository<Guid, TEntity, TContext>, IEntityRepository<TEntity>
where TEntity : IEntity
where TContext : IMongoDBContext
{
public MongoEntityRepository(TContext mongoDbContext, IApplicationContext applicationContext, IHistoryManager history) : base(mongoDbContext, applicationContext, history)
{
}
}