@@ -31,14 +31,19 @@ public abstract partial class ExpressiveCommandBase<TConnection, TCommand, TDbTy
31
31
/// </summary>
32
32
protected readonly TConnection Connection ;
33
33
34
+ /// <summary>
35
+ /// The transaction to execute commands on if not using a connection factory.
36
+ /// </summary>
37
+ protected readonly IDbTransaction Transaction ;
38
+
34
39
ExpressiveCommandBase (
35
40
CommandType type ,
36
41
string command ,
37
- List < Param > @params = null )
42
+ IEnumerable < Param > @params )
38
43
{
39
44
Type = type ;
40
45
Command = command ?? throw new ArgumentNullException ( nameof ( command ) ) ;
41
- Params = @params ?? throw new ArgumentNullException ( nameof ( @params ) ) ;
46
+ Params = @params ? . ToList ( ) ?? new List < Param > ( ) ;
42
47
Timeout = CommandTimeout . DEFAULT_SECONDS ;
43
48
}
44
49
@@ -50,25 +55,28 @@ protected ExpressiveCommandBase(
50
55
IDbConnectionFactory < TConnection > connFactory ,
51
56
CommandType type ,
52
57
string command ,
53
- List < Param > @params )
54
- : this ( type , command , @params ?? new List < Param > ( ) )
58
+ IEnumerable < Param > @params )
59
+ : this ( type , command , @params )
55
60
{
56
61
ConnectionFactory = connFactory ?? throw new ArgumentNullException ( nameof ( connFactory ) ) ;
57
62
}
58
63
59
- /// <param name="connection">The connection to execute the command on.</param>
60
- /// <param name="type">The command type>.</param>
61
- /// <param name="command">The SQL command.</param>
62
- /// <param name="params">The list of params</param>
63
- protected ExpressiveCommandBase (
64
+ /// <param name="connection">The connection to execute the command on.</param>
65
+ /// <param name="transaction">The optional transaction to execute the command on.</param>
66
+ /// <param name="type">The command type>.</param>
67
+ /// <param name="command">The SQL command.</param>
68
+ /// <param name="params">The list of params</param>
69
+ protected ExpressiveCommandBase (
64
70
TConnection connection ,
71
+ IDbTransaction transaction ,
65
72
CommandType type ,
66
73
string command ,
67
- List < Param > @params )
68
- : this ( type , command , @params ?? new List < Param > ( ) )
74
+ IEnumerable < Param > @params )
75
+ : this ( type , command , @params )
69
76
{
70
77
Connection = connection ?? throw new ArgumentNullException ( nameof ( connection ) ) ;
71
- }
78
+ Transaction = transaction ;
79
+ }
72
80
73
81
/// <summary>
74
82
/// The command text or procedure name to use.
@@ -252,36 +260,36 @@ public TThis SetTimeout(ushort seconds)
252
260
/// Handles providing the connection for use with the command.
253
261
/// </summary>
254
262
/// <param name="action">The handler for use with the connection.</param>
255
- protected void UsingConnection ( Action < TConnection > action )
263
+ protected void UsingConnection ( Action < TConnection , IDbTransaction > action )
256
264
{
257
- if ( ConnectionFactory != null )
258
- {
259
- using ( var conn = ConnectionFactory . Create ( ) )
260
- {
261
- action ( conn ) ;
262
- }
263
- }
264
- else
265
- {
266
- action ( Connection ) ;
267
- }
268
- }
265
+ if ( Connection != null )
266
+ {
267
+ action ( Connection , Transaction ) ;
268
+ }
269
+ else
270
+ {
271
+ using ( var conn = ConnectionFactory . Create ( ) )
272
+ {
273
+ action ( conn , null ) ;
274
+ }
275
+ }
276
+ }
269
277
270
278
/// <summary>
271
279
/// Handles providing the connection for use with the command.
272
280
/// </summary>
273
281
/// <param name="action">The handler for use with the connection.</param>
274
- protected T UsingConnection < T > ( Func < TConnection , T > action )
282
+ protected T UsingConnection < T > ( Func < TConnection , IDbTransaction , T > action )
275
283
{
276
284
if ( Connection != null )
277
285
{
278
- return action ( Connection ) ;
286
+ return action ( Connection , Transaction ) ;
279
287
}
280
288
else
281
289
{
282
290
using ( var conn = ConnectionFactory . Create ( ) )
283
291
{
284
- return action ( conn ) ;
292
+ return action ( conn , null ) ;
285
293
}
286
294
}
287
295
}
@@ -291,13 +299,14 @@ protected T UsingConnection<T>(Func<TConnection, T> action)
291
299
/// </summary>
292
300
/// <param name="handler">The handler function for each IDataRecord.</param>
293
301
public void Execute ( Action < TCommand > handler )
294
- => UsingConnection ( con =>
302
+ => UsingConnection ( ( con , t ) =>
295
303
{
296
304
using ( var cmd = con . CreateCommand ( Type , Command , Timeout ) )
297
305
{
298
306
var c = cmd as TCommand ;
299
307
if ( c == null ) throw new InvalidCastException ( $ "Actual command type ({ cmd . GetType ( ) } ) is not compatible with expected command type ({ typeof ( TCommand ) } ).") ;
300
- AddParams ( c ) ;
308
+ if ( t != null ) c . Transaction = t ;
309
+ AddParams ( c ) ;
301
310
con . EnsureOpen ( ) ;
302
311
handler ( c ) ;
303
312
}
@@ -311,13 +320,14 @@ public void Execute(Action<TCommand> handler)
311
320
/// <param name="transform">The transform function for each IDataRecord.</param>
312
321
/// <returns>The result of the transform.</returns>
313
322
public T Execute < T > ( Func < TCommand , T > transform )
314
- => UsingConnection ( con =>
323
+ => UsingConnection ( ( con , t ) =>
315
324
{
316
325
using ( var cmd = con . CreateCommand ( Type , Command , Timeout ) )
317
326
{
318
327
var c = cmd as TCommand ;
319
328
if ( c == null ) throw new InvalidCastException ( $ "Actual command type ({ cmd . GetType ( ) } ) is not compatible with expected command type ({ typeof ( TCommand ) } ).") ;
320
- AddParams ( c ) ;
329
+ if ( t != null ) c . Transaction = t ;
330
+ AddParams ( c ) ;
321
331
con . EnsureOpen ( ) ;
322
332
return transform ( c ) ;
323
333
}
@@ -329,13 +339,14 @@ public T Execute<T>(Func<TCommand, T> transform)
329
339
/// </summary>
330
340
/// <returns>The value from the return parameter.</returns>
331
341
public object ExecuteReturn ( )
332
- => UsingConnection ( con =>
342
+ => UsingConnection ( ( con , t ) =>
333
343
{
334
344
using ( var cmd = con . CreateCommand ( Type , Command , Timeout ) )
335
345
{
336
346
var c = cmd as TCommand ;
337
347
if ( c == null ) throw new InvalidCastException ( $ "Actual command type ({ cmd . GetType ( ) } ) is not compatible with expected command type ({ typeof ( TCommand ) } ).") ;
338
- AddParams ( c ) ;
348
+ if ( t != null ) c . Transaction = t ;
349
+ AddParams ( c ) ;
339
350
var returnParameter = c . CreateParameter ( ) ;
340
351
returnParameter . Direction = ParameterDirection . ReturnValue ;
341
352
c . Parameters . Add ( returnParameter ) ;
0 commit comments