-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericRepository.cs
More file actions
324 lines (293 loc) · 11.6 KB
/
Copy pathGenericRepository.cs
File metadata and controls
324 lines (293 loc) · 11.6 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// #define SYNC
#define CACHE
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Dapper;
using SqlKata;
using SqlKata.Compilers;
namespace Dapper.Issue2117.Test;
public class FallbackTypeMapper : SqlMapper.ITypeMap
{
private readonly IEnumerable<SqlMapper.ITypeMap> _mappers;
public FallbackTypeMapper(IEnumerable<SqlMapper.ITypeMap> mappers)
{
_mappers = mappers;
}
public ConstructorInfo? FindConstructor(string[] names, Type[] types)
{
foreach (var mapper in _mappers)
{
ConstructorInfo result = mapper.FindConstructor(names, types);
if (result != null)
return result;
}
return null;
}
public SqlMapper.IMemberMap? GetConstructorParameter(ConstructorInfo constructor, string columnName)
{
foreach (var mapper in _mappers)
{
var result = mapper.GetConstructorParameter(constructor, columnName);
if (result != null)
return result;
}
return null;
}
public SqlMapper.IMemberMap? GetMember(string columnName)
{
foreach (var mapper in _mappers)
{
var result = mapper.GetMember(columnName);
if (result != null)
return result;
}
return null;
}
public ConstructorInfo? FindExplicitConstructor()
{
return _mappers
.Select(mapper => mapper.FindExplicitConstructor())
.FirstOrDefault(result => result != null);
}
}
public class ColumnAttributeTypeMapper<T> : FallbackTypeMapper
{
public ColumnAttributeTypeMapper()
: base(new SqlMapper.ITypeMap[]
{
new CustomPropertyTypeMap(
typeof(T),
(type, columnName) =>
type.GetProperties().FirstOrDefault(prop =>
prop.GetCustomAttributes(false)
.OfType<ColumnAttribute>()
.Any(attr => attr.Name == columnName)
)
),
new DefaultTypeMap(typeof(T))
})
{
}
}
public interface IGenericRepository<TEntity, TKey>
{
string IdName { get; }
Task<TEntity> GetAsync(TKey id, IDbTransaction transaction, CancellationToken token);
Task<IEnumerable<TEntity>> GetAsync(IDbTransaction transaction, CancellationToken token);
Task<IEnumerable<TEntity>> GetAsync(int pageSize, int pageNo, IDbTransaction transaction, CancellationToken token);
Task<IEnumerable<TEntity>> GetAsync(IEnumerable<TEntity> filter, IDbTransaction transaction, CancellationToken token);
Task<TEntity> AddAsync(TEntity entity, IDbTransaction transaction, CancellationToken token);
Task<IEnumerable<TEntity>> AddAsync(IEnumerable<TEntity> entities, IDbTransaction transaction, CancellationToken token);
Task<TEntity> UpdateAsync(TEntity entity, IDbTransaction transaction, CancellationToken token);
Task<IEnumerable<TEntity>> UpdateAsync(IEnumerable<TEntity> entities, IDbTransaction transaction, CancellationToken token);
Task DeleteAsync(TEntity entity, IDbTransaction transaction, CancellationToken token);
Task DeleteAsync(IEnumerable<TEntity> entities, IDbTransaction transaction, CancellationToken token);
Task DeleteAsync(IDbTransaction transaction, CancellationToken token);
}
public abstract class GenericRepository<TEntity, TKey> where TEntity : class
{
protected readonly Compiler _compiler;
protected readonly string _tableName;
protected readonly string _idName;
protected readonly string _pkName;
private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
public GenericRepository(Compiler compiler, string tableName, string idName="Id", string pkName="id")
{
_compiler = compiler;
_tableName = tableName;
_idName = idName;
_pkName = pkName;
}
public virtual string IdName => _idName;
static GenericRepository()
{
Dapper.SqlMapper.SetTypeMap(
typeof(TEntity),
new ColumnAttributeTypeMapper<TEntity>());
}
protected CommandDefinition CompileQuery(Query query, IDbTransaction transaction,
CancellationToken token, CommandFlags flags = CommandFlags.Buffered)
{
var statement = _compiler.Compile(query).ToString();
return new CommandDefinition(statement, transaction:transaction,
cancellationToken:token, flags:flags);
}
protected async Task<IEnumerable<TEntity>> QueryAsync(Query query, IDbTransaction transaction, CancellationToken token)
{
#if SYNC
await _semaphore.WaitAsync().ConfigureAwait(false);
#endif
try
{
var command = CompileQuery(query, transaction, token,
#if CACHE
CommandFlags.Buffered);
#else
CommandFlags.Buffered | CommandFlags.NoCache);
#endif
return await transaction.Connection!.QueryAsync<TEntity>(command).ConfigureAwait(false);
}
finally
{
#if SYNC
SqlMapper.PurgeQueryCache();
_semaphore.Release();
#endif
}
}
protected async Task<IEnumerable<TEntity>> QueryAsync<TFirst, TSecond>(Query query, IDbTransaction transaction, CancellationToken token,
Func<TFirst, TSecond, TEntity> map, string splitOn = "id")
{
#if SYNC
await _semaphore.WaitAsync().ConfigureAwait(false);
#endif
try
{
var command = CompileQuery(query, transaction, token,
#if CACHE
CommandFlags.Buffered);
#else
CommandFlags.Buffered | CommandFlags.NoCache);
#endif
return await transaction.Connection!.QueryAsync(command, map, splitOn).ConfigureAwait(false);
}
finally
{
#if SYNC
SqlMapper.PurgeQueryCache();
_semaphore.Release();
#endif
}
}
private async Task<int> ExecuteAsync(Query query, IDbTransaction transaction, CancellationToken token)
{
#if SYNC
await _semaphore.WaitAsync().ConfigureAwait(false);
#endif
try
{
var command = CompileQuery(query, transaction, token,
#if CACHE
CommandFlags.Buffered);
#else
CommandFlags.Buffered | CommandFlags.NoCache);
#endif
return await transaction.Connection!.ExecuteAsync(command).ConfigureAwait(false);
}
finally
{
#if SYNC
SqlMapper.PurgeQueryCache();
_semaphore.Release();
#endif
}
}
protected virtual async Task<IEnumerable<TEntity>> MapAsync(Func<Query, Query> map, IDbTransaction transaction,
CancellationToken token)
{
var query = map(new Query(_tableName));
return await QueryAsync(query, transaction, token).ConfigureAwait(false);
}
public virtual async Task<TEntity> GetAsync(TKey id, IDbTransaction transaction,
CancellationToken token)
{
return (await MapAsync(query => query.Where(_tableName + "." + _pkName, id), transaction, token).ConfigureAwait(false)).Single();
}
public virtual async Task<IEnumerable<TEntity>> GetAsync(IDbTransaction transaction, CancellationToken token)
{
return await MapAsync(query => query, transaction, token).ConfigureAwait(false);
}
public virtual async Task<IEnumerable<TEntity>> GetAsync(int pageSize, int pageNo, IDbTransaction transaction, CancellationToken token)
{
return await MapAsync(query => query.Limit(pageSize).Offset(pageNo), transaction, token).ConfigureAwait(false);
}
public virtual async Task<IEnumerable<TEntity>> GetAsync(IEnumerable<TEntity> filter,
IDbTransaction transaction, CancellationToken token)
{
return await MapAsync(query => query.WhereIn(_tableName + "." + _pkName,
filter.Select(entity => entity.GetType().GetProperty(_idName)!.GetValue(entity))), transaction, token).ConfigureAwait(false);
}
public virtual async Task<TEntity> AddAsync(TEntity entity,
IDbTransaction transaction, CancellationToken token)
{
var query = new Query(_tableName).AsInsert(entity);
var result = await ExecuteAsync(query, transaction, token).ConfigureAwait(false);
if(result != 1)
throw new InvalidOperationException();
var id = entity.GetType().GetProperty(_idName)!.GetValue(entity)!;
return await GetAsync((TKey)id, transaction, token).ConfigureAwait(false);
}
public virtual async Task<IEnumerable<TEntity>> AddAsync(IEnumerable<TEntity> entities,
IDbTransaction transaction, CancellationToken token)
{
var result = 0;
foreach(var entity in entities)
{
var query = new Query(_tableName).AsInsert(entity);
result += await ExecuteAsync(query, transaction, token).ConfigureAwait(false);
}
if(result != entities.Count())
throw new InvalidOperationException();
return await GetAsync(entities, transaction, token).ConfigureAwait(false);
}
public virtual async Task<TEntity> UpdateAsync(TEntity entity,
IDbTransaction transaction, CancellationToken token)
{
var query = new Query(_tableName).Where(_pkName,
entity.GetType().GetProperty(_idName)!.GetValue(entity)).AsUpdate(entity);
var result = await ExecuteAsync(query, transaction, token).ConfigureAwait(false);
if(result != 1)
throw new InvalidOperationException();
var id = entity.GetType().GetProperty(_idName)!.GetValue(entity)!;
return await GetAsync((TKey)id, transaction, token).ConfigureAwait(false);
}
public virtual async Task<IEnumerable<TEntity>> UpdateAsync(IEnumerable<TEntity> entities,
IDbTransaction transaction, CancellationToken token)
{
var result = 0;
foreach(var entity in entities)
{
var query = new Query(_tableName).Where(_pkName,
entity.GetType().GetProperty(_idName)!.GetValue(entity)).AsUpdate(entity);
result += await ExecuteAsync(query, transaction, token).ConfigureAwait(false);
}
if(result != entities.Count())
throw new InvalidOperationException();
return await GetAsync(entities, transaction, token).ConfigureAwait(false);
}
public virtual async Task DeleteAsync(TEntity entity,
IDbTransaction transaction, CancellationToken token)
{
var query = new Query(_tableName).Where(_pkName,
entity.GetType().GetProperty(_idName)!.GetValue(entity)).AsDelete();
var result = await ExecuteAsync(query, transaction, token).ConfigureAwait(false);
if(result != 1)
throw new InvalidOperationException();
}
public virtual async Task DeleteAsync(IEnumerable<TEntity> entities,
IDbTransaction transaction, CancellationToken token)
{
var result = 0;
foreach(var entity in entities)
{
var query = new Query(_tableName).Where(_pkName,
entity.GetType().GetProperty(_idName)!.GetValue(entity)).AsDelete();
result += await ExecuteAsync(query, transaction, token).ConfigureAwait(false);
}
if(result != entities.Count())
throw new InvalidOperationException();
}
public virtual async Task DeleteAsync(IDbTransaction transaction, CancellationToken token)
{
var count = (await GetAsync(transaction, token).ConfigureAwait(false)).Count();
var query = new Query(_tableName).AsDelete();
var result = await ExecuteAsync(query, transaction, token).ConfigureAwait(false);
if(result != count)
throw new InvalidOperationException();
}
}