Skip to content

Commit 7d80d0b

Browse files
author
electricessence
committed
Added more generic Func<TConn> for factory usage. Allows for generating a connection without creating a class implementation or using new DbConnectionFactory()
1 parent ece018f commit 7d80d0b

File tree

4 files changed

+109
-8
lines changed

4 files changed

+109
-8
lines changed

DbConnectionFactory.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,18 @@ public DbConnectionFactory(Func<TConnection> factory)
2929

3030
IDbConnection IDbConnectionFactory.Create() => Create();
3131
}
32+
33+
/// <summary>
34+
/// DbConnection factory implementation that accepts a factory function.
35+
/// </summary>
36+
public class DbConnectionFactory : DbConnectionFactory<DbConnection>
37+
{
38+
/// <summary>
39+
/// Constructs a DbConnectionFactory.
40+
/// </summary>
41+
/// <param name="factory"></param>
42+
public DbConnectionFactory(Func<DbConnection> factory) : base(factory)
43+
{
44+
}
45+
}
3246
}

Documentation.xml

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Extensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,5 +1371,29 @@ public static ExpressiveDbCommand StoredProcedure(
13711371
this IDbConnectionFactory<DbConnection> target,
13721372
string command)
13731373
=> new ExpressiveDbCommand(target, CommandType.StoredProcedure, command);
1374+
1375+
/// <summary>
1376+
/// Creates an ExpressiveDbCommand for subsequent configuration and execution.
1377+
/// </summary>
1378+
/// <param name="target">The connection factory to generate a commands from.</param>
1379+
/// <param name="command">The command text or stored procedure name to use.</param>
1380+
/// <param name="type">The command type.</param>
1381+
/// <returns>The resultant ExpressiveDbCommand.</returns>
1382+
public static ExpressiveDbCommand Command(
1383+
this Func<DbConnection> target,
1384+
string command,
1385+
CommandType type = CommandType.Text)
1386+
=> Command(new DbConnectionFactory(target), command, type);
1387+
1388+
/// <summary>
1389+
/// Creates an ExpressiveDbCommand with command type set to StoredProcedure for subsequent configuration and execution.
1390+
/// </summary>
1391+
/// <param name="target">The connection factory to generate a commands from.</param>
1392+
/// <param name="command">The command text or stored procedure name to use.</param>
1393+
/// <returns>The resultant ExpressiveDbCommand.</returns>
1394+
public static ExpressiveDbCommand StoredProcedure(
1395+
this Func<DbConnection> target,
1396+
string command)
1397+
=> StoredProcedure(new DbConnectionFactory(target), command);
13741398
}
13751399
}

SqlClient/Extensions.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11

22
using System;
3-
using System.Collections.Generic;
43
using System.Data;
5-
using System.Data.Common;
64
using System.Data.SqlClient;
7-
using System.Linq;
8-
using System.Threading;
9-
using System.Threading.Tasks;
10-
using System.Threading.Tasks.Dataflow;
115

126
namespace Open.Database.Extensions.SqlClient
137
{
@@ -82,7 +76,7 @@ public static SqlParameter AddParameterType(this SqlCommand target,
8276
target.Parameters.Add(c);
8377
return c;
8478
}
85-
79+
8680
/// <summary>
8781
/// Shortcut for adding command a typed (non-input) parameter.
8882
/// </summary>
@@ -94,7 +88,7 @@ public static SqlParameter AddParameterType(this IDbCommand target, string name,
9488
{
9589
return AddParameterType((SqlCommand)target, name, type);
9690
}
97-
91+
9892
/// <summary>
9993
/// Creates an ExpressiveSqlCommand for subsequent configuration and execution.
10094
/// </summary>
@@ -121,5 +115,29 @@ public static ExpressiveSqlCommand StoredProcedure(
121115
{
122116
return new ExpressiveSqlCommand(target, CommandType.StoredProcedure, command);
123117
}
118+
119+
/// <summary>
120+
/// Creates an ExpressiveSqlCommand for subsequent configuration and execution.
121+
/// </summary>
122+
/// <param name="target">The connection factory to generate a commands from.</param>
123+
/// <param name="command">The command text or stored procedure name to use.</param>
124+
/// <param name="type">The command type.</param>
125+
/// <returns>The resultant ExpressiveSqlCommand.</returns>
126+
public static ExpressiveSqlCommand Command(
127+
this Func<SqlConnection> target,
128+
string command, CommandType type = CommandType.Text)
129+
=> Command(new DbConnectionFactory<SqlConnection>(target), command, type);
130+
131+
/// <summary>
132+
/// Creates an ExpressiveSqlCommand with command type set to StoredProcedure for subsequent configuration and execution.
133+
/// </summary>
134+
/// <param name="target">The connection factory to generate a commands from.</param>
135+
/// <param name="command">The command text or stored procedure name to use.</param>
136+
/// <returns>The resultant ExpressiveSqlCommand.</returns>
137+
public static ExpressiveSqlCommand StoredProcedure(
138+
this Func<SqlConnection> target,
139+
string command)
140+
=> StoredProcedure(new DbConnectionFactory<SqlConnection>(target), command);
141+
124142
}
125143
}

0 commit comments

Comments
 (0)