@@ -31,14 +31,19 @@ public abstract partial class ExpressiveCommandBase<TConnection, TCommand, TDbTy
3131 /// </summary>
3232 protected readonly TConnection Connection ;
3333
34+ /// <summary>
35+ /// The transaction to execute commands on if not using a connection factory.
36+ /// </summary>
37+ protected readonly IDbTransaction Transaction ;
38+
3439 ExpressiveCommandBase (
3540 CommandType type ,
3641 string command ,
37- List < Param > @params = null )
42+ IEnumerable < Param > @params )
3843 {
3944 Type = type ;
4045 Command = command ?? throw new ArgumentNullException ( nameof ( command ) ) ;
41- Params = @params ?? throw new ArgumentNullException ( nameof ( @params ) ) ;
46+ Params = @params ? . ToList ( ) ?? new List < Param > ( ) ;
4247 Timeout = CommandTimeout . DEFAULT_SECONDS ;
4348 }
4449
@@ -50,25 +55,28 @@ protected ExpressiveCommandBase(
5055 IDbConnectionFactory < TConnection > connFactory ,
5156 CommandType type ,
5257 string command ,
53- List < Param > @params )
54- : this ( type , command , @params ?? new List < Param > ( ) )
58+ IEnumerable < Param > @params )
59+ : this ( type , command , @params )
5560 {
5661 ConnectionFactory = connFactory ?? throw new ArgumentNullException ( nameof ( connFactory ) ) ;
5762 }
5863
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 (
6470 TConnection connection ,
71+ IDbTransaction transaction ,
6572 CommandType type ,
6673 string command ,
67- List < Param > @params )
68- : this ( type , command , @params ?? new List < Param > ( ) )
74+ IEnumerable < Param > @params )
75+ : this ( type , command , @params )
6976 {
7077 Connection = connection ?? throw new ArgumentNullException ( nameof ( connection ) ) ;
71- }
78+ Transaction = transaction ;
79+ }
7280
7381 /// <summary>
7482 /// The command text or procedure name to use.
@@ -252,36 +260,36 @@ public TThis SetTimeout(ushort seconds)
252260 /// Handles providing the connection for use with the command.
253261 /// </summary>
254262 /// <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 )
256264 {
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+ }
269277
270278 /// <summary>
271279 /// Handles providing the connection for use with the command.
272280 /// </summary>
273281 /// <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 )
275283 {
276284 if ( Connection != null )
277285 {
278- return action ( Connection ) ;
286+ return action ( Connection , Transaction ) ;
279287 }
280288 else
281289 {
282290 using ( var conn = ConnectionFactory . Create ( ) )
283291 {
284- return action ( conn ) ;
292+ return action ( conn , null ) ;
285293 }
286294 }
287295 }
@@ -291,13 +299,14 @@ protected T UsingConnection<T>(Func<TConnection, T> action)
291299 /// </summary>
292300 /// <param name="handler">The handler function for each IDataRecord.</param>
293301 public void Execute ( Action < TCommand > handler )
294- => UsingConnection ( con =>
302+ => UsingConnection ( ( con , t ) =>
295303 {
296304 using ( var cmd = con . CreateCommand ( Type , Command , Timeout ) )
297305 {
298306 var c = cmd as TCommand ;
299307 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 ) ;
301310 con . EnsureOpen ( ) ;
302311 handler ( c ) ;
303312 }
@@ -311,13 +320,14 @@ public void Execute(Action<TCommand> handler)
311320 /// <param name="transform">The transform function for each IDataRecord.</param>
312321 /// <returns>The result of the transform.</returns>
313322 public T Execute < T > ( Func < TCommand , T > transform )
314- => UsingConnection ( con =>
323+ => UsingConnection ( ( con , t ) =>
315324 {
316325 using ( var cmd = con . CreateCommand ( Type , Command , Timeout ) )
317326 {
318327 var c = cmd as TCommand ;
319328 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 ) ;
321331 con . EnsureOpen ( ) ;
322332 return transform ( c ) ;
323333 }
@@ -329,13 +339,14 @@ public T Execute<T>(Func<TCommand, T> transform)
329339 /// </summary>
330340 /// <returns>The value from the return parameter.</returns>
331341 public object ExecuteReturn ( )
332- => UsingConnection ( con =>
342+ => UsingConnection ( ( con , t ) =>
333343 {
334344 using ( var cmd = con . CreateCommand ( Type , Command , Timeout ) )
335345 {
336346 var c = cmd as TCommand ;
337347 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 ) ;
339350 var returnParameter = c . CreateParameter ( ) ;
340351 returnParameter . Direction = ParameterDirection . ReturnValue ;
341352 c . Parameters . Add ( returnParameter ) ;
0 commit comments