@@ -120,6 +120,75 @@ public static async Task<ConnectionState> EnsureOpenAsync(this DbConnection conn
120
120
return state ;
121
121
}
122
122
123
+ /// <summary>
124
+ /// Generates a connection and executes the action within a using statement.
125
+ /// Useful for single-line operations.
126
+ /// </summary>
127
+ /// <typeparam name="TConn">The connection type.</typeparam>
128
+ /// <typeparam name="T">The type returned from the action.</typeparam>
129
+ /// <param name="connectionFactory">The connection factory to generate connections from.</param>
130
+ /// <param name="action">The action to execute.</param>
131
+ /// <returns>The value from the action.</returns>
132
+ public static T Using < TConn , T > ( this IDbConnectionFactory < TConn > connectionFactory , Func < TConn , T > action )
133
+ where TConn : IDbConnection
134
+ {
135
+ using ( var conn = connectionFactory . Create ( ) )
136
+ {
137
+ return action ( conn ) ;
138
+ }
139
+ }
140
+
141
+ /// <summary>
142
+ /// Generates a connection and executes the action within a using statement.
143
+ /// Useful for single-line operations.
144
+ /// </summary>
145
+ /// <typeparam name="TConn">The connection type.</typeparam>
146
+ /// <param name="connectionFactory">The connection factory to generate connections from.</param>
147
+ /// <param name="action">The action to execute.</param>
148
+ public static void Using < TConn > ( this IDbConnectionFactory < TConn > connectionFactory , Action < TConn > action )
149
+ where TConn : IDbConnection
150
+ {
151
+ using ( var conn = connectionFactory . Create ( ) )
152
+ {
153
+ action ( conn ) ;
154
+ }
155
+ }
156
+
157
+ /// <summary>
158
+ /// Generates a connection and executes the action within a using statement.
159
+ /// Useful for single-line operations.
160
+ /// </summary>
161
+ /// <typeparam name="TConn">The connection type.</typeparam>
162
+ /// <typeparam name="T">The type returned from the action.</typeparam>
163
+ /// <param name="connectionFactory">The connection factory to generate connections from.</param>
164
+ /// <param name="action">The action to execute.</param>
165
+ /// <returns>The value from the action.</returns>
166
+ public static T Using < TConn , T > ( this Func < TConn > connectionFactory , Func < TConn , T > action )
167
+ where TConn : IDbConnection
168
+ {
169
+ using ( var conn = connectionFactory ( ) )
170
+ {
171
+ return action ( conn ) ;
172
+ }
173
+ }
174
+
175
+ /// <summary>
176
+ /// Generates a connection and executes the action within a using statement.
177
+ /// Useful for single-line operations.
178
+ /// </summary>
179
+ /// <typeparam name="TConn">The connection type.</typeparam>
180
+ /// <param name="connectionFactory">The connection factory to generate connections from.</param>
181
+ /// <param name="action">The action to execute.</param>
182
+ public static void Using < TConn > ( this Func < TConn > connectionFactory , Action < TConn > action )
183
+ where TConn : IDbConnection
184
+ {
185
+ using ( var conn = connectionFactory ( ) )
186
+ {
187
+ action ( conn ) ;
188
+ }
189
+ }
190
+
191
+
123
192
/// <summary>
124
193
/// Shortcut for creating an IDbCommand from any IDbConnection.
125
194
/// </summary>
0 commit comments