-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoDbDataRepository.cs
119 lines (109 loc) · 4.98 KB
/
MongoDbDataRepository.cs
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
using System.Collections.Generic;
using System.Threading.Tasks;
using Collections.API.Factories.Interfaces;
using Collections.API.Repositories.Interfaces;
using MongoDB.Driver;
namespace Collections.API.Repositories
{
/// <summary>
/// Repository for data from the document db
/// </summary>
/// <seealso cref="Collections.API.Repositories.Interfaces.IDataRepository" />
public class MongoDbDataRepository : IDataRepository
{
/// <summary>
/// The mongo DB factory
/// </summary>
private readonly IMongoDbFactory mongoFactory;
/// <summary>
/// Initializes a new instance of the <see cref="MongoDbDataRepository"/> class.
/// </summary>
/// <param name="mongoFactory">The mongo DB factory.</param>
public MongoDbDataRepository(IMongoDbFactory mongoFactory)
{
this.mongoFactory = mongoFactory;
}
/// <summary>
/// Finds one record asynchronously.
/// </summary>
/// <typeparam name="TInterface">The filter type</typeparam>
/// <param name="filter">The filter.</param>
/// <returns>Retrieved record</returns>
public async Task<TInterface> FindOneAsync<TInterface>(FilterDefinition<TInterface> filter)
{
return await this.RetrieveCollection<TInterface>().Find(filter).FirstAsync();
}
/// <summary>
/// Finds many records asynchronously.
/// </summary>
/// <typeparam name="TInterface">The filter type</typeparam>
/// <param name="filter">The filter.</param>
/// <returns>Retrived records</returns>
public async Task<IEnumerable<TInterface>> FindManyAsync<TInterface>(FilterDefinition<TInterface> filter)
{
return await this.RetrieveCollection<TInterface>().Find(filter).ToListAsync();
}
/// <summary>
/// Inserts one record asynchronously.
/// </summary>
/// <typeparam name="TInterface">The filter type</typeparam>
/// <typeparam name="TModel">The model type</typeparam>
/// <param name="model">The model.</param>
public async Task InsertOneAsync<TInterface, TModel>(TModel model) where TModel : class, TInterface, new()
{
await this.RetrieveCollection<TInterface>().InsertOneAsync(model);
}
/// <summary>
/// Inserts many records asynchronously.
/// </summary>
/// <typeparam name="TInterface">The filter type</typeparam>
/// <typeparam name="TModel">The model type</typeparam>
/// <param name="models">The models.</param>
public async Task InsertManyAsync<TInterface, TModel>(IEnumerable<TModel> models) where TModel : class, TInterface, new()
{
await this.RetrieveCollection<TInterface>().InsertManyAsync(models);
}
/// <summary>
/// Updates one record asynchronously.
/// </summary>
/// <typeparam name="TInterface">The filter type</typeparam>
/// <param name="filter">The filter.</param>
/// <param name="update">The update.</param>
/// <returns><see cref="UpdateResult"/>result of update operation</returns>
public async Task<UpdateResult> UpdateOneAsync<TInterface>(FilterDefinition<TInterface> filter, UpdateDefinition<TInterface> update)
{
return await this.RetrieveCollection<TInterface>().UpdateOneAsync(filter, update);
}
/// <summary>
/// Replaces one document asynchronously.
/// </summary>
/// <typeparam name="TInterface">The filter type</typeparam>
/// <typeparam name="TModel">The model type</typeparam>
/// <param name="filter">The filter.</param>
/// <param name="model">The model.</param>
/// <returns><see cref="ReplaceOneResult"/>result of replace operation</returns>
public async Task<ReplaceOneResult> ReplaceOneAsync<TInterface, TModel>(FilterDefinition<TInterface> filter, TModel model) where TModel : class, TInterface, new()
{
return await this.RetrieveCollection<TInterface>().ReplaceOneAsync(filter, model);
}
/// <summary>
/// Deletes many records asynchronously.
/// </summary>
/// <typeparam name="TInterface">The filter type</typeparam>
/// <param name="filter">The filter.</param>
/// <returns><see cref="DeleteResult"/>result of delete operation</returns>
public async Task<DeleteResult> DeleteManyAsync<TInterface>(FilterDefinition<TInterface> filter)
{
return await this.RetrieveCollection<TInterface>().DeleteManyAsync(filter);
}
/// <summary>
/// Retrieves the collection.
/// </summary>
/// <typeparam name="TInterface">Model of collection</typeparam>
/// <returns>Mongo DB collection</returns>
private IMongoCollection<TInterface> RetrieveCollection<TInterface>()
{
return this.mongoFactory.ConnectToCollection<TInterface>();
}
}
}