Skip to content

Commit 8c4f8cf

Browse files
author
electricessence
committed
Added improved AddParameter extensions.
1 parent 7782058 commit 8c4f8cf

5 files changed

Lines changed: 137 additions & 20 deletions

File tree

Documentation.xml

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

ExpressiveAsyncCommandBase.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,23 @@ protected ExpressiveAsyncCommandBase(
7979
/// <typeparam name="T">The type expected.</typeparam>
8080
/// <returns>The varlue returned from the method.</returns>
8181
public async Task<T> ExecuteScalarAsync<T>()
82-
{
83-
return (T)(await ExecuteScalarAsync());
84-
}
82+
=> (T)(await ExecuteScalarAsync());
83+
84+
/// <summary>
85+
/// Asynchronously executes scalar on the underlying command.
86+
/// </summary>
87+
/// <typeparam name="T">The type expected.</typeparam>
88+
/// <returns>The varlue returned from the method.</returns>
89+
public async Task<T> ExecuteScalarAsync<T>(Func<object, T> transform)
90+
=> transform(await ExecuteScalarAsync());
91+
92+
/// <summary>
93+
/// Asynchronously executes scalar on the underlying command.
94+
/// </summary>
95+
/// <typeparam name="T">The type expected.</typeparam>
96+
/// <returns>The varlue returned from the method.</returns>
97+
public async Task<T> ExecuteScalarAsync<T>(Func<object, Task<T>> transform)
98+
=> await transform(await ExecuteScalarAsync());
8599

86100
/// <summary>
87101
/// Asynchronously iterates a IDataReader and returns the each result until the count is met.

ExpressiveCommandBase.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,19 @@ public object ExecuteScalar()
427427
public T ExecuteScalar<T>()
428428
=> (T)ExecuteScalar();
429429

430-
/// <summary>
431-
/// Imports all data using an IDataReader into a DataTable.
432-
/// </summary>
433-
/// <returns>The resultant DataTabel.</returns>
434-
public DataTable LoadTable()
430+
/// <summary>
431+
/// Calls ExecuteScalar on the underlying command.
432+
/// </summary>
433+
/// <typeparam name="T">The type expected.</typeparam>
434+
/// <returns>The varlue returned from the method.</returns>
435+
public T ExecuteScalar<T>(Func<object,T> transform)
436+
=> transform(ExecuteScalar());
437+
438+
/// <summary>
439+
/// Imports all data using an IDataReader into a DataTable.
440+
/// </summary>
441+
/// <returns>The resultant DataTabel.</returns>
442+
public DataTable LoadTable()
435443
=> Execute(command => command.ToDataTable());
436444

437445
/// <summary>

Extensions.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ public static IDbDataParameter AddParameter(this IDbCommand target,
4747
/// <param name="name">The name of the parameter.</param>
4848
/// <param name="value">The value of the parameter.</param>
4949
/// <param name="type">The DbType of the parameter.</param>
50+
/// <param name="direction">The direction of the parameter.</param>
5051
/// <returns>The created IDbDataParameter.</returns>
5152
public static IDbDataParameter AddParameter(this IDbCommand target,
52-
string name, object value, DbType type)
53+
string name, object value, DbType type, ParameterDirection direction = ParameterDirection.Input)
5354
{
5455
var p = target.AddParameterType(name, type);
5556
p.Value = value;
@@ -62,13 +63,15 @@ public static IDbDataParameter AddParameter(this IDbCommand target,
6263
/// <param name="target">The command to add a parameter to.</param>
6364
/// <param name="name">The name of the parameter.</param>
6465
/// <param name="type">The DbType of the parameter.</param>
66+
/// <param name="direction">The direction of the parameter.</param>
6567
/// <returns>The created IDbDataParameter.</returns>
6668
public static IDbDataParameter AddParameterType(this IDbCommand target,
67-
string name, DbType type)
69+
string name, DbType type, ParameterDirection direction = ParameterDirection.Input)
6870
{
6971
var c = target.CreateParameter();
7072
c.ParameterName = name;
7173
c.DbType = type;
74+
c.Direction = direction;
7275
target.Parameters.Add(c);
7376
return c;
7477
}
@@ -94,6 +97,25 @@ public static IDbCommand CreateCommand(this IDbConnection connection,
9497
return command;
9598
}
9699

100+
/// <summary>
101+
/// Shortcut for creating an IDbCommand from any IDbConnection of CommandType.StoredProcedure.
102+
/// </summary>
103+
/// <param name="connection">The connection to create a command from.</param>
104+
/// <param name="commandText">The command text or stored procedure name to use.</param>
105+
/// <param name="secondsTimeout">The number of seconds to wait before the command times out.</param>
106+
/// <returns>The created IDbCommand.</returns>
107+
public static IDbCommand CreateStoredProcedureCommand(this IDbConnection connection,
108+
string commandText,
109+
int secondsTimeout = CommandTimeout.DEFAULT_SECONDS)
110+
{
111+
var command = connection.CreateCommand();
112+
command.CommandType = CommandType.StoredProcedure;
113+
command.CommandText = commandText;
114+
command.CommandTimeout = secondsTimeout;
115+
116+
return command;
117+
}
118+
97119
/// <summary>
98120
/// Iterates all records from an IDataReader.
99121
/// </summary>

SqlClient/Extensions.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ public static class Extensions
2323
/// <param name="name">The name of the parameter.</param>
2424
/// <param name="value">The value of the parameter.</param>
2525
/// <param name="type">The DbType of the parameter.</param>
26+
/// <param name="direction">The direction of the parameter.</param>
2627
/// <returns>The created IDbDataParameter.</returns>
27-
public static IDbDataParameter AddParameter(this SqlCommand target,
28-
string name, object value, SqlDbType type)
28+
public static SqlParameter AddParameter(this SqlCommand target,
29+
string name, object value, SqlDbType type, ParameterDirection direction = ParameterDirection.Input)
2930
{
30-
var p = target.AddParameterType(name, type);
31+
var p = target.AddParameterType(name, type, direction);
3132
p.Value = value;
3233
return p;
3334
}
@@ -38,24 +39,27 @@ public static IDbDataParameter AddParameter(this SqlCommand target,
3839
/// <param name="target">The command to add a parameter to.</param>
3940
/// <param name="name">The name of the parameter.</param>
4041
/// <param name="type">The SqlDbType of the parameter.</param>
42+
/// <param name="direction">The direction of the parameter.</param>
4143
/// <returns>The created IDbDataParameter.</returns>
42-
public static IDbDataParameter AddParameterType(this SqlCommand target, string name, SqlDbType type)
44+
public static SqlParameter AddParameterType(this SqlCommand target,
45+
string name, SqlDbType type, ParameterDirection direction = ParameterDirection.Input)
4346
{
4447
var c = target.CreateParameter();
4548
c.ParameterName = name;
4649
c.SqlDbType = type;
50+
c.Direction = direction;
4751
target.Parameters.Add(c);
4852
return c;
4953
}
50-
54+
5155
/// <summary>
5256
/// Shortcut for adding command a typed (non-input) parameter.
5357
/// </summary>
5458
/// <param name="target">The command to add a parameter to.</param>
5559
/// <param name="name">The name of the parameter.</param>
5660
/// <param name="type">The SqlDbType of the parameter.</param>
5761
/// <returns>The created IDbDataParameter.</returns>
58-
public static IDbDataParameter AddParameterType(this IDbCommand target, string name, SqlDbType type)
62+
public static SqlParameter AddParameterType(this IDbCommand target, string name, SqlDbType type)
5963
{
6064
return AddParameterType((SqlCommand)target, name, type);
6165
}
@@ -79,6 +83,24 @@ public static SqlCommand CreateCommand(this SqlConnection connection,
7983
return command;
8084
}
8185

86+
/// <summary>
87+
/// Shortcut for creating an SqlCommand from any SqlConnection.
88+
/// </summary>
89+
/// <param name="connection">The connection to create a command from.</param>
90+
/// <param name="commandText">The command text or stored procedure name to use.</param>
91+
/// <param name="secondsTimeout">The number of seconds to wait before the command times out.</param>
92+
/// <returns>The created SqlCommand.</returns>
93+
public static SqlCommand CreateStoredProcedureCommand(this SqlConnection connection,
94+
string commandText, int secondsTimeout = CommandTimeout.DEFAULT_SECONDS)
95+
{
96+
var command = connection.CreateCommand();
97+
command.CommandType = CommandType.StoredProcedure;
98+
command.CommandText = commandText;
99+
command.CommandTimeout = secondsTimeout;
100+
101+
return command;
102+
}
103+
82104
/// <summary>
83105
/// Asynchronously iterates all records using an IDataReader and returns the desired results as a list.
84106
/// </summary>

0 commit comments

Comments
 (0)