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

loresoft / MongoDB.Abstracts / 13118055932

03 Feb 2025 04:24PM UTC coverage: 77.474% (-1.4%) from 78.879%
13118055932

push

github

pwelter34
add tests, update readme

32 of 64 branches covered (50.0%)

Branch coverage included in aggregate %.

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

21 existing lines in 4 files now uncovered.

195 of 229 relevant lines covered (85.15%)

7.63 hits per line

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

76.64
/src/MongoDB.Abstracts/MongoRepository.cs
1
// Ignore Spelling: Mongo
2

3
using System.Linq.Expressions;
4

5
using MongoDB.Driver;
6

7
namespace MongoDB.Abstracts;
8

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

24
    }
15✔
25

26
    /// <inheritdoc/>
27
    public TEntity Insert(TEntity entity)
28
    {
29
        if (entity == null)
5!
UNCOV
30
            throw new ArgumentNullException(nameof(entity));
×
31

32
        BeforeInsert(entity);
5✔
33
        Collection.InsertOne(entity);
5✔
34

35
        return entity;
5✔
36
    }
37

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

44
        BeforeInsert(entity);
12✔
45

46
        await Collection
12✔
47
            .InsertOneAsync(entity, cancellationToken: cancellationToken)
12✔
48
            .ConfigureAwait(false);
12✔
49

50
        return entity;
12✔
51
    }
12✔
52

53

54
    /// <inheritdoc/>
55
    public void InsertBatch(IEnumerable<TEntity> entities)
56
    {
57
        if (entities == null)
1!
UNCOV
58
            throw new ArgumentNullException(nameof(entities));
×
59

60
        var list = entities.ToList();
1✔
61
        list.ForEach(BeforeInsert);
1✔
62

63
        Collection.InsertMany(list);
1✔
64
    }
1✔
65

66

67
    /// <inheritdoc/>
68
    public TEntity Update(TEntity entity)
69
    {
70
        if (entity == null)
4!
UNCOV
71
            throw new ArgumentNullException(nameof(entity));
×
72

73
        BeforeUpdate(entity);
4✔
74

75
        var options = new ReplaceOptions();
4✔
76
        var key = EntityKey(entity);
4✔
77
        var filter = KeyExpression(key);
4✔
78

79
        Collection.ReplaceOne(filter, entity, options);
4✔
80

81
        return entity;
4✔
82
    }
83

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

90
        BeforeUpdate(entity);
5✔
91

92
        var options = new ReplaceOptions();
5✔
93
        var key = EntityKey(entity);
5✔
94
        var filter = KeyExpression(key);
5✔
95

96
        await Collection
5✔
97
            .ReplaceOneAsync(filter, entity, options, cancellationToken)
5✔
98
            .ConfigureAwait(false);
5✔
99

100
        return entity;
5✔
101
    }
5✔
102

103

104

105
    /// <inheritdoc/>
106
    public TEntity Upsert(TEntity entity)
107
    {
108
        if (entity == null)
2!
UNCOV
109
            throw new ArgumentNullException(nameof(entity));
×
110

111
        BeforeUpdate(entity);
2✔
112

113
        var options = new ReplaceOptions { IsUpsert = true };
2✔
114
        var key = EntityKey(entity);
2✔
115
        var filter = KeyExpression(key);
2✔
116

117
        Collection.ReplaceOne(filter, entity, options);
2✔
118

119
        return entity;
2✔
120
    }
121

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

128
        BeforeUpdate(entity);
2✔
129

130
        var options = new ReplaceOptions { IsUpsert = true };
2✔
131
        var key = EntityKey(entity);
2✔
132
        var filter = KeyExpression(key);
2✔
133

134
        await Collection
2✔
135
            .ReplaceOneAsync(filter, entity, options, cancellationToken)
2✔
136
            .ConfigureAwait(false);
2✔
137

138
        return entity;
2✔
139
    }
2✔
140

141

142
    /// <inheritdoc/>
143
    public long Delete(TKey id)
144
    {
145
        if (id == null)
4!
UNCOV
146
            throw new ArgumentNullException(nameof(id));
×
147

148
        var filter = KeyExpression(id);
4✔
149

150
        var result = Collection.DeleteOne(filter);
4✔
151

152
        return result.DeletedCount;
4✔
153
    }
154

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

161
        var filter = KeyExpression(id);
5✔
162

163
        var result = await Collection
5✔
164
            .DeleteOneAsync(filter, cancellationToken)
5✔
165
            .ConfigureAwait(false);
5✔
166

167
        return result.DeletedCount;
5✔
168
    }
5✔
169

170

171
    /// <inheritdoc/>
172
    public long Delete(TEntity entity)
173
    {
174
        if (entity == null)
4!
UNCOV
175
            throw new ArgumentNullException(nameof(entity));
×
176

177
        var key = EntityKey(entity);
4✔
178

179
        return Delete(key);
4✔
180
    }
181

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

188
        var key = EntityKey(entity);
4✔
189

190
        return await DeleteAsync(key, cancellationToken).ConfigureAwait(false);
4✔
191
    }
4✔
192

193

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

200
        var result = Collection.DeleteMany(criteria);
1✔
201

202
        return result.DeletedCount;
1✔
203
    }
204

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

211
        var result = await Collection
1✔
212
            .DeleteManyAsync(criteria, cancellationToken)
1✔
213
            .ConfigureAwait(false);
1✔
214

215
        return result.DeletedCount;
1✔
216
    }
1✔
217

218

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

229
        mongoEntity.Created = DateTimeOffset.UtcNow;
19✔
230
        mongoEntity.Updated = DateTimeOffset.UtcNow;
19✔
231
    }
19✔
232

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

243
        if (mongoEntity.Created == default)
13!
244
            mongoEntity.Created = DateTimeOffset.UtcNow;
×
245

246
        mongoEntity.Updated = DateTimeOffset.UtcNow;
13✔
247
    }
13✔
248
}
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