-
Notifications
You must be signed in to change notification settings - Fork 580
Expand file tree
/
Copy pathDapperExtensions.cs
More file actions
426 lines (376 loc) · 19.9 KB
/
DapperExtensions.cs
File metadata and controls
426 lines (376 loc) · 19.9 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
using DapperExtensions.Mapper;
using DapperExtensions.Predicate;
using DapperExtensions.Sql;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace DapperExtensions
{
public static class DapperExtensions
{
private readonly static object _lock = new object();
private static Func<IDapperExtensionsConfiguration, IDapperImplementor> _instanceFactory;
private static IDapperImplementor _instance;
private static IDapperExtensionsConfiguration _configuration;
/// <summary>
/// Gets or sets the default class mapper to use when generating class maps. If not specified, AutoClassMapper<T> is used.
/// DapperExtensions.Configure(Type, IList<Assembly>, ISqlDialect) can be used instead to set all values at once
/// </summary>
public static Type DefaultMapper
{
get
{
return _configuration.DefaultMapper;
}
set
{
Configure(value, _configuration.MappingAssemblies, SqlDialect);
}
}
/// <summary>
/// Gets or sets the type of sql to be generated.
/// DapperExtensions.Configure(Type, IList<Assembly>, ISqlDialect) can be used instead to set all values at once
/// </summary>
public static ISqlDialect SqlDialect
{
get
{
return _configuration.Dialect;
}
set
{
Configure(DefaultMapper, _configuration.MappingAssemblies, value);
}
}
/// <summary>
/// Get or sets the Dapper Extensions Implementation Factory.
/// </summary>
public static Func<IDapperExtensionsConfiguration, IDapperImplementor> InstanceFactory
{
get
{
return _instanceFactory ??= config => new DapperImplementor(new ThreadSafeSqlGeneratorImpl(config));
}
set
{
_instanceFactory = value;
Configure(_configuration.DefaultMapper, _configuration.MappingAssemblies, _configuration.Dialect);
}
}
public static SqlInjection GetOrSetSqlInjection(this Type entityType, SqlInjection sqlInjection = null)
{
return _configuration.GetOrSetSqlInjection(entityType, sqlInjection);
}
/// <summary>
/// Gets the Dapper Extensions Implementation
/// </summary>
private static IDapperImplementor Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
_instance = InstanceFactory(_configuration);
}
}
}
return _instance;
}
}
/// <summary>
/// Configure DapperExtensions extension methods.
/// </summary>
/// <param name="defaultMapper"></param>
/// <param name="mappingAssemblies"></param>
/// <param name="sqlDialect"></param>
public static void Configure(this IDapperExtensionsConfiguration configuration)
{
_instance = null;
_configuration = configuration;
}
static DapperExtensions()
{
Configure(typeof(AutoClassMapper<>), new List<Assembly>(), new SqlServerDialect());
}
/// <summary>
/// Add other assemblies that Dapper Extensions will search if a mapping is not found in the same assembly of the POCO.
/// </summary>
/// <param name="assemblies"></param>
public static void SetMappingAssemblies(this IList<Assembly> assemblies)
{
Configure(_configuration.DefaultMapper, assemblies, _configuration.Dialect);
}
/// <summary>
/// Configure DapperExtensions extension methods.
/// </summary>
/// <param name="defaultMapper"></param>
/// <param name="mappingAssemblies"></param>
/// <param name="sqlDialect"></param>
public static void Configure(this Type defaultMapper, IList<Assembly> mappingAssemblies, ISqlDialect sqlDialect)
{
Configure(new DapperExtensionsConfiguration(defaultMapper, mappingAssemblies, sqlDialect));
}
/// <summary>
/// Executes a query for the specified id, returning the data typed as per T
/// </summary>
public static T Get<T>(this IDbConnection connection, dynamic id, IDbTransaction transaction = null, int? commandTimeout = null)
{
return (T)Instance.Get<T>(connection, id, transaction, commandTimeout);
}
/// <summary>
/// Executes a query for the specified id, returning the data typed as per T
/// </summary>
public static TOut GetPartial<TIn, TOut>(this IDbConnection connection, dynamic id, Expression<Func<TIn, TOut>> func, IDbTransaction transaction = null, int? commandTimeout = null) where TIn : class where TOut : class
{
return Instance.GetPartial<TIn, TOut>(connection, func, id, transaction, commandTimeout);
}
/// <summary>
/// Executes an insert query for the specified entity.
/// </summary>
public static void Insert<T>(this IDbConnection connection, IEnumerable<T> entities, IDbTransaction transaction = null, int? commandTimeout = null)
{
Instance.Insert(connection, entities, transaction, commandTimeout);
}
/// <summary>
/// Executes an insert query for the specified entity, returning the primary key.
/// If the entity has a single key, just the value is returned.
/// If the entity has a composite key, an IDictionary<string, object> is returned with the key values.
/// The key value for the entity will also be updated if the KeyType is a Guid or Identity.
/// </summary>
public static dynamic Insert<T>(this IDbConnection connection, T entity, IDbTransaction transaction = null, int? commandTimeout = null)
{
return Instance.Insert(connection, entity, transaction, commandTimeout);
}
/// <summary>
/// Executes an update query for the specified entity.
/// </summary>
public static bool Update<T>(this IDbConnection connection, T entity, IDbTransaction transaction = null, int? commandTimeout = null, bool ignoreAllKeyProperties = false)
{
return Instance.Update(connection, entity, transaction, commandTimeout, ignoreAllKeyProperties);
}
/// <summary>
/// Executes an update query for the specified entity.
/// </summary>
public static void Update<T>(this IDbConnection connection, IEnumerable<T> entities, IDbTransaction transaction = null, int? commandTimeout = null, bool ignoreAllKeyProperties = false)
{
Instance.Update(connection, entities, transaction, commandTimeout, ignoreAllKeyProperties);
}
/// <summary>
/// Executes some column an update query for the specified entity, as typed by LINq expresion
/// </summary>
public static bool UpdatePartial<TIn, TOut>(this IDbConnection connection, TIn entity, Expression<Func<TIn, TOut>> func, IDbTransaction transaction = null, int? commandTimeout = null, bool ignoreAllKeyProperties = false) where TIn : class where TOut : class
{
return Instance.UpdatePartial(connection, entity, func, transaction, commandTimeout, ignoreAllKeyProperties);
}
/// <summary>
/// Executes some column an update query for the specified entity, as typed by LINq expresion
/// </summary>
public static void UpdatePartial<TIn, TOut>(this IDbConnection connection, IEnumerable<TIn> entities, Expression<Func<TIn, TOut>> func, IDbTransaction transaction = null, int? commandTimeout = null, bool ignoreAllKeyProperties = false) where TIn : class where TOut : class
{
Instance.UpdatePartial(connection, entities, func, transaction, commandTimeout, ignoreAllKeyProperties);
}
/// <summary>
/// Executes a delete query for the specified entity.
/// </summary>
public static bool Delete<T>(this IDbConnection connection, T entity, IDbTransaction transaction = null, int? commandTimeout = null)
{
return Instance.Delete(connection, entity, transaction, commandTimeout);
}
/// <summary>
/// Executes a delete query for the specified entity.
/// </summary>
public static void Delete<T>(this IDbConnection connection, IEnumerable<T> entities, IDbTransaction transaction = null, int? commandTimeout = null)
{
Instance.Delete(connection, entities, transaction, commandTimeout);
}
/// <summary>
/// Executes a delete query using the specified predicate.
/// </summary>
public static bool Delete<T>(this IDbConnection connection, object predicate, IDbTransaction transaction = null, int? commandTimeout = null)
{
return Instance.Delete<T>(connection, predicate, transaction, commandTimeout);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T.
/// </summary>
public static IEnumerable<T> GetList<T>(this IDbConnection connection, object predicate = null, IList<ISort> sort = null, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false)
{
return Instance.GetList<T>(connection, predicate, sort, transaction, commandTimeout, buffered);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per LINq Expression.
/// </summary>
public static IEnumerable<TOut> GetPartialList<TIn, TOut>(this IDbConnection connection, Expression<Func<TIn, TOut>> func, object predicate = null, IList<ISort> sort = null, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false) where TIn : class
{
return Instance.GetPartialList(connection, func, predicate, sort, transaction, commandTimeout, buffered);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T with relacionated classes.
/// </summary>
public static IEnumerable<T> GetListAutoMap<T>(this IDbConnection connection, object predicate = null, IList<ISort> sort = null, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false)
{
return Instance.GetListAutoMap<T>(connection, predicate, sort, transaction, commandTimeout, buffered);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T with relacionated classes.
/// </summary>
public static IEnumerable<TOut> GetPartialListAutoMap<TIn, TOut>(this IDbConnection connection, Expression<Func<TIn, TOut>> func, object predicate = null, IList<ISort> sort = null, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false) where TIn : class
{
return Instance.GetPartialListAutoMap(connection, func, predicate, sort, transaction, commandTimeout, buffered);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T.
/// Data returned is dependent upon the specified page and resultsPerPage.
/// </summary>
public static IEnumerable<T> GetPage<T>(this IDbConnection connection, object predicate, IList<ISort> sort, int page, int resultsPerPage, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false)
{
return Instance.GetPage<T>(connection, predicate, sort, page, resultsPerPage, transaction, commandTimeout, buffered);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per LINq expression.
/// Data returned is dependent upon the specified page and resultsPerPage.
/// </summary>
public static IEnumerable<TOut> GetPartialPage<TIn, TOut>(this IDbConnection connection, Expression<Func<TIn, TOut>> func, object predicate, IList<ISort> sort, int page, int resultsPerPage, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false) where TIn : class where TOut : class
{
return Instance.GetPartialPage(connection, func, predicate, sort, page, resultsPerPage, transaction, commandTimeout, buffered);
}
public static IEnumerable<T> GetPageAutoMap<T>(this IDbConnection connection, object predicate, IList<ISort> sort, int page, int resultsPerPage, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false)
{
return Instance.GetPageAutoMap<T>(connection, predicate, sort, page, resultsPerPage, transaction, commandTimeout, buffered);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per LINq expression.
/// Data returned is dependent upon the specified page and resultsPerPage.
/// </summary>
public static IEnumerable<TOut> GetPartialPageAutoMap<TIn, TOut>(this IDbConnection connection, Expression<Func<TIn, TOut>> func, object predicate, IList<ISort> sort, int page, int resultsPerPage, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false) where TIn : class where TOut : class
{
return Instance.GetPartialPageAutoMap(connection, func, predicate, sort, page, resultsPerPage, transaction, commandTimeout, buffered);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T.
/// Data returned is dependent upon the specified firstResult and maxResults.
/// </summary>
public static IEnumerable<T> GetSet<T>(this IDbConnection connection, object predicate, IList<ISort> sort, int firstResult, int maxResults, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false)
{
return Instance.GetSet<T>(connection, predicate, sort, firstResult, maxResults, transaction, commandTimeout, buffered);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per LINq expression.
/// Data returned is dependent upon the specified firstResult and maxResults.
/// </summary>
public static IEnumerable<TOut> GetPartialSet<TIn, TOut>(this IDbConnection connection, Expression<Func<TIn, TOut>> func, object predicate, IList<ISort> sort, int firstResult, int maxResults, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false) where TIn : class where TOut : class
{
return Instance.GetPartialSet(connection, func, predicate, sort, firstResult, maxResults, transaction, commandTimeout, buffered);
}
/// <summary>
/// Executes a query using the specified predicate, returning an integer that represents the number of rows that match the query.
/// </summary>
public static int Count<T>(this IDbConnection connection, object predicate, IDbTransaction transaction = null, int? commandTimeout = null)
{
return Instance.Count<T>(connection, predicate, transaction, commandTimeout);
}
/// <summary>
/// Executes a select query for multiple objects, returning IMultipleResultReader for each predicate.
/// </summary>
public static IMultipleResultReader GetMultiple(this IDbConnection connection, GetMultiplePredicate predicate, IDbTransaction transaction = null, int? commandTimeout = null)
{
return Instance.GetMultiple(connection, predicate, transaction, commandTimeout);
}
/// <summary>
/// Gets the appropriate mapper for the specified type T.
/// If the mapper for the type is not yet created, a new mapper is generated from the mapper type specifed by DefaultMapper.
/// </summary>
public static IClassMapper GetMap<T>()
{
return _configuration.GetMap<T>();
}
public static IClassMapper GetMap(this Type entityType)
{
return _configuration.GetMap(entityType);
}
public static IList<Assembly> MappingAssemblies { get => _configuration.MappingAssemblies; }
public static Type GetMapType(this Type entityType)
{
return Instance.SqlGenerator.Configuration.GetMapType(entityType);
}
/// <summary>
/// Clears the ClassMappers for each type.
/// </summary>
public static void ClearCache()
{
Instance.SqlGenerator.Configuration.ClearCache();
}
/// <summary>
/// Generates a COMB Guid which solves the fragmented index issue.
/// See: http://davybrion.com/blog/2009/05/using-the-guidcomb-identifier-strategy
/// </summary>
public static Guid GetNextGuid()
{
return Instance.SqlGenerator.Configuration.GetNextGuid();
}
/// <summary>Gets columns from class mapped</summary>
/// <param name="map">IClassMapper from class mapped</param>
/// <returns>Array from ColumnName</returns>
public static List<IColumn> AllMappedColumns { get; }
public static IEnumerable<IMemberMap> GetIdentifiers(this IClassMapper map)
{
foreach (var item in map.Properties)
{
if (item.Name != item.Name
&& (item.KeyType == KeyType.Identity ||
item.KeyType == KeyType.Assigned ||
item.KeyType == KeyType.SequenceIdentity)
)
{
yield return item;
}
}
foreach (var table in Instance.SqlGenerator.MappedTables)
{
var refs = DapperExtensions.GetMap(table.EntityType)
.Properties
.Where(c => c.ClassMapper.Identity == table.Identity && (c.KeyType == KeyType.Identity ||
c.KeyType == KeyType.Assigned ||
c.KeyType == KeyType.SequenceIdentity));
foreach (var r in refs)
{
yield return r;
}
}
}
/// <summary>
/// Returns the name of the database table based on mappings done with POCO.
/// Generic <T> is POCO class.
/// See: https://stackoverflow.com/a/49321116/5779732
/// </summary>
public static string GetTableName<T>()
{
return new SqlGeneratorImpl(_configuration).GetTableName(GetMap<T>());
}
/// <summary>
/// Returns the name of the database column based on mappings done with POCO.
/// 'propertyName' is name of the property in POCO for which the column name should be returned.
/// Generic <T> is POCO class.
/// See: https://stackoverflow.com/a/49321116/5779732
/// </summary>
public static string GetColumnName<T>(string propertyName)
{
return new SqlGeneratorImpl(_configuration).GetColumnName(GetMap<T>(), propertyName, false);
}
/// <summary>
/// Gets the last SQL command executed by the Dapper Extensions Implementation
/// </summary>
public static string LastExecutedCommand()
{
return Instance.LastExecutedCommand;
}
}
}