• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / MongoDB.Abstracts / 9036919010

10 May 2024 07:08PM UTC coverage: 78.879% (+1.0%) from 77.844%
9036919010

push

github

pwelter34
update build and tests

26 of 50 branches covered (52.0%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

6 existing lines in 1 file now uncovered.

157 of 182 relevant lines covered (86.26%)

8.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.64
/src/MongoDB.Abstracts/MongoRepository.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Linq.Expressions;
5
using System.Threading;
6
using System.Threading.Tasks;
7

8
using MongoDB.Driver;
9

10
namespace MongoDB.Abstracts;
11

12
/// <summary>
13
/// A MongoDB data repository base class.
14
/// </summary>
15
/// <typeparam name="TEntity">The type of the entity.</typeparam>
16
/// <typeparam name="TKey">The type of the key.</typeparam>
17
public abstract class MongoRepository<TEntity, TKey> : MongoQuery<TEntity, TKey>, IMongoRepository<TEntity, TKey>
18
    where TEntity : class
19
{
20
    /// <summary>
21
    /// Initializes a new instance of the <see cref="MongoRepository{TEntity, TKey}"/> class.
22
    /// </summary>
23
    /// <exception cref="ArgumentNullException"><paramref name="mongoDatabase"/> is <see langword="null" />.</exception>
24
    protected MongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase)
14✔
25
    {
26

27
    }
14✔
28

29
    /// <inheritdoc/>
30
    public TEntity Insert(TEntity entity)
31
    {
32
        if (entity == null)
5!
33
            throw new ArgumentNullException(nameof(entity));
×
34

35
        BeforeInsert(entity);
5✔
36
        Collection.InsertOne(entity);
5✔
37

38
        return entity;
5✔
39
    }
40

41
    /// <inheritdoc/>
42
    public async Task<TEntity> InsertAsync(TEntity entity, CancellationToken cancellationToken = default)
43
    {
44
        if (entity == null)
12!
UNCOV
45
            throw new ArgumentNullException(nameof(entity));
×
46

47
        BeforeInsert(entity);
12✔
48

49
        await Collection
12✔
50
            .InsertOneAsync(entity, cancellationToken: cancellationToken)
12✔
51
            .ConfigureAwait(false);
12✔
52

53
        return entity;
12✔
54
    }
12✔
55

56

57
    /// <inheritdoc/>
58
    public void InsertBatch(IEnumerable<TEntity> entities)
59
    {
60
        if (entities == null)
1!
61
            throw new ArgumentNullException(nameof(entities));
×
62

63
        var list = entities.ToList();
1✔
64
        list.ForEach(BeforeInsert);
1✔
65

66
        Collection.InsertMany(list);
1✔
67
    }
1✔
68

69

70
    /// <inheritdoc/>
71
    public TEntity Update(TEntity entity)
72
    {
73
        if (entity == null)
4!
74
            throw new ArgumentNullException(nameof(entity));
×
75

76
        BeforeUpdate(entity);
4✔
77

78
        var options = new ReplaceOptions();
4✔
79
        var key = EntityKey(entity);
4✔
80
        var filter = KeyExpression(key);
4✔
81

82
        Collection.ReplaceOne(filter, entity, options);
4✔
83

84
        return entity;
4✔
85
    }
86

87
    /// <inheritdoc/>
88
    public async Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
89
    {
90
        if (entity == null)
5!
UNCOV
91
            throw new ArgumentNullException(nameof(entity));
×
92

93
        BeforeUpdate(entity);
5✔
94

95
        var options = new ReplaceOptions();
5✔
96
        var key = EntityKey(entity);
5✔
97
        var filter = KeyExpression(key);
5✔
98

99
        await Collection
5✔
100
            .ReplaceOneAsync(filter, entity, options, cancellationToken)
5✔
101
            .ConfigureAwait(false);
5✔
102

103
        return entity;
5✔
104
    }
5✔
105

106

107

108
    /// <inheritdoc/>
109
    public TEntity Upsert(TEntity entity)
110
    {
111
        if (entity == null)
2!
112
            throw new ArgumentNullException(nameof(entity));
×
113

114
        BeforeUpdate(entity);
2✔
115

116
        var options = new ReplaceOptions { IsUpsert = true };
2✔
117
        var key = EntityKey(entity);
2✔
118
        var filter = KeyExpression(key);
2✔
119

120
        Collection.ReplaceOne(filter, entity, options);
2✔
121

122
        return entity;
2✔
123
    }
124

125
    /// <inheritdoc/>
126
    public async Task<TEntity> UpsertAsync(TEntity entity, CancellationToken cancellationToken = default)
127
    {
128
        if (entity == null)
2!
UNCOV
129
            throw new ArgumentNullException(nameof(entity));
×
130

131
        BeforeUpdate(entity);
2✔
132

133
        var options = new ReplaceOptions { IsUpsert = true };
2✔
134
        var key = EntityKey(entity);
2✔
135
        var filter = KeyExpression(key);
2✔
136

137
        await Collection
2✔
138
            .ReplaceOneAsync(filter, entity, options, cancellationToken)
2✔
139
            .ConfigureAwait(false);
2✔
140

141
        return entity;
2✔
142
    }
2✔
143

144

145
    /// <inheritdoc/>
146
    public long Delete(TKey id)
147
    {
148
        if (id == null)
4!
149
            throw new ArgumentNullException(nameof(id));
×
150

151
        var filter = KeyExpression(id);
4✔
152

153
        var result = Collection.DeleteOne(filter);
4✔
154

155
        return result.DeletedCount;
4✔
156
    }
157

158
    /// <inheritdoc/>
159
    public async Task<long> DeleteAsync(TKey id, CancellationToken cancellationToken = default)
160
    {
161
        if (id == null)
5!
UNCOV
162
            throw new ArgumentNullException(nameof(id));
×
163

164
        var filter = KeyExpression(id);
5✔
165

166
        var result = await Collection
5✔
167
            .DeleteOneAsync(filter, cancellationToken)
5✔
168
            .ConfigureAwait(false);
5✔
169

170
        return result.DeletedCount;
5✔
171
    }
5✔
172

173

174
    /// <inheritdoc/>
175
    public long Delete(TEntity entity)
176
    {
177
        if (entity == null)
4!
178
            throw new ArgumentNullException(nameof(entity));
×
179

180
        var key = EntityKey(entity);
4✔
181

182
        return Delete(key);
4✔
183
    }
184

185
    /// <inheritdoc/>
186
    public async Task<long> DeleteAsync(TEntity entity, CancellationToken cancellationToken = default)
187
    {
188
        if (entity == null)
4!
UNCOV
189
            throw new ArgumentNullException(nameof(entity));
×
190

191
        var key = EntityKey(entity);
4✔
192

193
        return await DeleteAsync(key, cancellationToken).ConfigureAwait(false);
4✔
194
    }
4✔
195

196

197
    /// <inheritdoc/>
198
    public long DeleteAll(Expression<Func<TEntity, bool>> criteria)
199
    {
200
        if (criteria == null)
1!
201
            throw new ArgumentNullException(nameof(criteria));
×
202

203
        var result = Collection.DeleteMany(criteria);
1✔
204

205
        return result.DeletedCount;
1✔
206
    }
207

208
    /// <inheritdoc/>
209
    public async Task<long> DeleteAllAsync(Expression<Func<TEntity, bool>> criteria, CancellationToken cancellationToken = default)
210
    {
211
        if (criteria == null)
1!
UNCOV
212
            throw new ArgumentNullException(nameof(criteria));
×
213

214
        var result = await Collection
1✔
215
            .DeleteManyAsync(criteria, cancellationToken)
1✔
216
            .ConfigureAwait(false);
1✔
217

218
        return result.DeletedCount;
1✔
219
    }
1✔
220

221

222
    /// <summary>
223
    /// Called before an insert.
224
    /// </summary>
225
    /// <param name="entity">The entity that is being inserted.</param>
226
    protected virtual void BeforeInsert(TEntity entity)
227
    {
228
        var mongoEntity = entity as IMongoEntity;
19✔
229
        if (mongoEntity == null)
19!
230
            return;
×
231

232
        mongoEntity.Created = DateTimeOffset.UtcNow;
19✔
233
        mongoEntity.Updated = DateTimeOffset.UtcNow;
19✔
234
    }
19✔
235

236
    /// <summary>
237
    /// Called before an update.
238
    /// </summary>
239
    /// <param name="entity">The entity that is being updated.</param>
240
    protected virtual void BeforeUpdate(TEntity entity)
241
    {
242
        var mongoEntity = entity as IMongoEntity;
13✔
243
        if (mongoEntity == null)
13!
244
            return;
×
245

246
        if (mongoEntity.Created == default)
13!
247
            mongoEntity.Created = DateTimeOffset.UtcNow;
×
248

249
        mongoEntity.Updated = DateTimeOffset.UtcNow;
13✔
250
    }
13✔
251
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc