@@ -12,7 +12,7 @@ namespace Dapper.Contrib.Extensions
12
12
public static partial class SqlMapperExtensions
13
13
{
14
14
/// <summary>
15
- /// Returns a single entity by a single id from table "Ts" asynchronously using .NET 4.5 Task. T must be of interface type.
15
+ /// Returns a single entity by a single id from table "Ts" asynchronously using Task. T must be of interface type.
16
16
/// Id must be marked with [Key] attribute.
17
17
/// Created entity is tracked/intercepted for changes and used by the Update() extension.
18
18
/// </summary>
@@ -34,24 +34,24 @@ public static async Task<T> GetAsync<T>(this IDbConnection connection, dynamic i
34
34
GetQueries [ type . TypeHandle ] = sql ;
35
35
}
36
36
37
- var dynParms = new DynamicParameters ( ) ;
38
- dynParms . Add ( "@id" , id ) ;
37
+ var dynParams = new DynamicParameters ( ) ;
38
+ dynParams . Add ( "@id" , id ) ;
39
39
40
- if ( ! type . IsInterface ( ) )
41
- return ( await connection . QueryAsync < T > ( sql , dynParms , transaction , commandTimeout ) . ConfigureAwait ( false ) ) . FirstOrDefault ( ) ;
40
+ if ( ! type . IsInterface )
41
+ return ( await connection . QueryAsync < T > ( sql , dynParams , transaction , commandTimeout ) . ConfigureAwait ( false ) ) . FirstOrDefault ( ) ;
42
42
43
- var res = ( await connection . QueryAsync < dynamic > ( sql , dynParms ) . ConfigureAwait ( false ) ) . FirstOrDefault ( ) as IDictionary < string , object > ;
44
-
45
- if ( res == null )
43
+ if ( ! ( ( await connection . QueryAsync < dynamic > ( sql , dynParams ) . ConfigureAwait ( false ) ) . FirstOrDefault ( ) is IDictionary < string , object > res ) )
44
+ {
46
45
return null ;
46
+ }
47
47
48
48
var obj = ProxyGenerator . GetInterfaceProxy < T > ( ) ;
49
49
50
50
foreach ( var property in TypePropertiesCache ( type ) )
51
51
{
52
52
var val = res [ property . Name ] ;
53
53
if ( val == null ) continue ;
54
- if ( property . PropertyType . IsGenericType ( ) && property . PropertyType . GetGenericTypeDefinition ( ) == typeof ( Nullable < > ) )
54
+ if ( property . PropertyType . IsGenericType && property . PropertyType . GetGenericTypeDefinition ( ) == typeof ( Nullable < > ) )
55
55
{
56
56
var genericType = Nullable . GetUnderlyingType ( property . PropertyType ) ;
57
57
if ( genericType != null ) property . SetValue ( obj , Convert . ChangeType ( val , genericType ) , null ) ;
@@ -68,7 +68,7 @@ public static async Task<T> GetAsync<T>(this IDbConnection connection, dynamic i
68
68
}
69
69
70
70
/// <summary>
71
- /// Returns a list of entites from table "Ts".
71
+ /// Returns a list of entities from table "Ts".
72
72
/// Id of T must be marked with [Key] attribute.
73
73
/// Entities created from interfaces are tracked/intercepted for changes and used by the Update() extension
74
74
/// for optimal performance.
@@ -92,7 +92,7 @@ public static Task<IEnumerable<T>> GetAllAsync<T>(this IDbConnection connection,
92
92
GetQueries [ cacheType . TypeHandle ] = sql ;
93
93
}
94
94
95
- if ( ! type . IsInterface ( ) )
95
+ if ( ! type . IsInterface )
96
96
{
97
97
return connection . QueryAsync < T > ( sql , null , transaction , commandTimeout ) ;
98
98
}
@@ -101,7 +101,7 @@ public static Task<IEnumerable<T>> GetAllAsync<T>(this IDbConnection connection,
101
101
102
102
private static async Task < IEnumerable < T > > GetAllAsyncImpl < T > ( IDbConnection connection , IDbTransaction transaction , int ? commandTimeout , string sql , Type type ) where T : class
103
103
{
104
- var result = await connection . QueryAsync ( sql ) . ConfigureAwait ( false ) ;
104
+ var result = await connection . QueryAsync ( sql , transaction : transaction , commandTimeout : commandTimeout ) . ConfigureAwait ( false ) ;
105
105
var list = new List < T > ( ) ;
106
106
foreach ( IDictionary < string , object > res in result )
107
107
{
@@ -110,7 +110,7 @@ private static async Task<IEnumerable<T>> GetAllAsyncImpl<T>(IDbConnection conne
110
110
{
111
111
var val = res [ property . Name ] ;
112
112
if ( val == null ) continue ;
113
- if ( property . PropertyType . IsGenericType ( ) && property . PropertyType . GetGenericTypeDefinition ( ) == typeof ( Nullable < > ) )
113
+ if ( property . PropertyType . IsGenericType && property . PropertyType . GetGenericTypeDefinition ( ) == typeof ( Nullable < > ) )
114
114
{
115
115
var genericType = Nullable . GetUnderlyingType ( property . PropertyType ) ;
116
116
if ( genericType != null ) property . SetValue ( obj , Convert . ChangeType ( val , genericType ) , null ) ;
@@ -127,7 +127,7 @@ private static async Task<IEnumerable<T>> GetAllAsyncImpl<T>(IDbConnection conne
127
127
}
128
128
129
129
/// <summary>
130
- /// Inserts an entity into table "Ts" asynchronously using .NET 4.5 Task and returns identity id.
130
+ /// Inserts an entity into table "Ts" asynchronously using Task and returns identity id.
131
131
/// </summary>
132
132
/// <typeparam name="T">The type being inserted.</typeparam>
133
133
/// <param name="connection">Open SqlConnection</param>
@@ -140,19 +140,19 @@ public static Task<int> InsertAsync<T>(this IDbConnection connection, T entityTo
140
140
int ? commandTimeout = null , ISqlAdapter sqlAdapter = null ) where T : class
141
141
{
142
142
var type = typeof ( T ) ;
143
- sqlAdapter = sqlAdapter ?? GetFormatter ( connection ) ;
143
+ sqlAdapter ??= GetFormatter ( connection ) ;
144
144
145
145
var isList = false ;
146
146
if ( type . IsArray )
147
147
{
148
148
isList = true ;
149
149
type = type . GetElementType ( ) ;
150
150
}
151
- else if ( type . IsGenericType ( ) )
151
+ else if ( type . IsGenericType )
152
152
{
153
153
var typeInfo = type . GetTypeInfo ( ) ;
154
154
bool implementsGenericIEnumerableOrIsGenericIEnumerable =
155
- typeInfo . ImplementedInterfaces . Any ( ti => ti . IsGenericType ( ) && ti . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > ) ) ||
155
+ typeInfo . ImplementedInterfaces . Any ( ti => ti . IsGenericType && ti . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > ) ) ||
156
156
typeInfo . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > ) ;
157
157
158
158
if ( implementsGenericIEnumerableOrIsGenericIEnumerable )
@@ -198,7 +198,7 @@ public static Task<int> InsertAsync<T>(this IDbConnection connection, T entityTo
198
198
}
199
199
200
200
/// <summary>
201
- /// Updates entity in table "Ts" asynchronously using .NET 4.5 Task, checks if the entity is modified if the entity is tracked by the Get() extension.
201
+ /// Updates entity in table "Ts" asynchronously using Task, checks if the entity is modified if the entity is tracked by the Get() extension.
202
202
/// </summary>
203
203
/// <typeparam name="T">Type to be updated</typeparam>
204
204
/// <param name="connection">Open SqlConnection</param>
@@ -219,11 +219,11 @@ public static async Task<bool> UpdateAsync<T>(this IDbConnection connection, T e
219
219
{
220
220
type = type . GetElementType ( ) ;
221
221
}
222
- else if ( type . IsGenericType ( ) )
222
+ else if ( type . IsGenericType )
223
223
{
224
224
var typeInfo = type . GetTypeInfo ( ) ;
225
225
bool implementsGenericIEnumerableOrIsGenericIEnumerable =
226
- typeInfo . ImplementedInterfaces . Any ( ti => ti . IsGenericType ( ) && ti . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > ) ) ||
226
+ typeInfo . ImplementedInterfaces . Any ( ti => ti . IsGenericType && ti . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > ) ) ||
227
227
typeInfo . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > ) ;
228
228
229
229
if ( implementsGenericIEnumerableOrIsGenericIEnumerable )
@@ -269,7 +269,7 @@ public static async Task<bool> UpdateAsync<T>(this IDbConnection connection, T e
269
269
}
270
270
271
271
/// <summary>
272
- /// Delete entity in table "Ts" asynchronously using .NET 4.5 Task.
272
+ /// Delete entity in table "Ts" asynchronously using Task.
273
273
/// </summary>
274
274
/// <typeparam name="T">Type of entity</typeparam>
275
275
/// <param name="connection">Open SqlConnection</param>
@@ -288,11 +288,11 @@ public static async Task<bool> DeleteAsync<T>(this IDbConnection connection, T e
288
288
{
289
289
type = type . GetElementType ( ) ;
290
290
}
291
- else if ( type . IsGenericType ( ) )
291
+ else if ( type . IsGenericType )
292
292
{
293
293
var typeInfo = type . GetTypeInfo ( ) ;
294
294
bool implementsGenericIEnumerableOrIsGenericIEnumerable =
295
- typeInfo . ImplementedInterfaces . Any ( ti => ti . IsGenericType ( ) && ti . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > ) ) ||
295
+ typeInfo . ImplementedInterfaces . Any ( ti => ti . IsGenericType && ti . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > ) ) ||
296
296
typeInfo . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > ) ;
297
297
298
298
if ( implementsGenericIEnumerableOrIsGenericIEnumerable )
@@ -307,26 +307,26 @@ public static async Task<bool> DeleteAsync<T>(this IDbConnection connection, T e
307
307
throw new ArgumentException ( "Entity must have at least one [Key] or [ExplicitKey] property" ) ;
308
308
309
309
var name = GetTableName ( type ) ;
310
- keyProperties . AddRange ( explicitKeyProperties ) ;
310
+ var allKeyProperties = keyProperties . Concat ( explicitKeyProperties ) . ToList ( ) ;
311
311
312
312
var sb = new StringBuilder ( ) ;
313
313
sb . AppendFormat ( "DELETE FROM {0} WHERE " , name ) ;
314
314
315
315
var adapter = GetFormatter ( connection ) ;
316
-
317
- for ( var i = 0 ; i < keyProperties . Count ; i ++ )
316
+
317
+ for ( var i = 0 ; i < allKeyProperties . Count ; i ++ )
318
318
{
319
- var property = keyProperties [ i ] ;
319
+ var property = allKeyProperties [ i ] ;
320
320
adapter . AppendColumnNameEqualsValue ( sb , property . Name ) ;
321
- if ( i < keyProperties . Count - 1 )
321
+ if ( i < allKeyProperties . Count - 1 )
322
322
sb . Append ( " AND " ) ;
323
323
}
324
324
var deleted = await connection . ExecuteAsync ( sb . ToString ( ) , entityToDelete , transaction , commandTimeout ) . ConfigureAwait ( false ) ;
325
325
return deleted > 0 ;
326
326
}
327
327
328
328
/// <summary>
329
- /// Delete all entities in the table related to the type T asynchronously using .NET 4.5 Task.
329
+ /// Delete all entities in the table related to the type T asynchronously using Task.
330
330
/// </summary>
331
331
/// <typeparam name="T">Type of entity</typeparam>
332
332
/// <param name="connection">Open SqlConnection</param>
@@ -379,7 +379,7 @@ public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction tran
379
379
var cmd = $ "INSERT INTO { tableName } ({ columnList } ) values ({ parameterList } ); SELECT SCOPE_IDENTITY() id";
380
380
var multi = await connection . QueryMultipleAsync ( cmd , entityToInsert , transaction , commandTimeout ) . ConfigureAwait ( false ) ;
381
381
382
- var first = multi . Read ( ) . FirstOrDefault ( ) ;
382
+ var first = await multi . ReadFirstOrDefaultAsync ( ) . ConfigureAwait ( false ) ;
383
383
if ( first == null || first . id == null ) return 0 ;
384
384
385
385
var id = ( int ) first . id ;
@@ -499,7 +499,7 @@ public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction tran
499
499
500
500
var results = await connection . QueryAsync ( sb . ToString ( ) , entityToInsert , transaction , commandTimeout ) . ConfigureAwait ( false ) ;
501
501
502
- // Return the key by assinging the corresponding property in the object - by product is that it supports compound primary keys
502
+ // Return the key by assigning the corresponding property in the object - by product is that it supports compound primary keys
503
503
var id = 0 ;
504
504
foreach ( var p in propertyInfos )
505
505
{
@@ -531,7 +531,7 @@ public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction tran
531
531
var cmd = $ "INSERT INTO { tableName } ({ columnList } ) VALUES ({ parameterList } ); SELECT last_insert_rowid() id";
532
532
var multi = await connection . QueryMultipleAsync ( cmd , entityToInsert , transaction , commandTimeout ) . ConfigureAwait ( false ) ;
533
533
534
- var id = ( int ) multi . Read ( ) . First ( ) . id ;
534
+ var id = ( int ) ( await multi . ReadFirstAsync ( ) . ConfigureAwait ( false ) ) . id ;
535
535
var pi = keyProperties as PropertyInfo [ ] ?? keyProperties . ToArray ( ) ;
536
536
if ( pi . Length == 0 ) return id ;
537
537
0 commit comments