Skip to content

Commit e11470c

Browse files
author
Oren (electricessence)
committed
2 parents 0f09337 + 9dab97e commit e11470c

File tree

3 files changed

+96
-15
lines changed

3 files changed

+96
-15
lines changed

Documentation.xml

Lines changed: 25 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ExpressiveCommandBase.cs

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,41 @@ public T Execute<T>(Func<TCommand, T> transform)
270270
}
271271
}
272272

273-
/// <summary>
274-
/// Internal reader for simplifying iteration. If exposed publicly could potentially hold connections open because an iteration may have not completed.
275-
/// </summary>
276-
/// <typeparam name="T">The return type of the transform function.</typeparam>
277-
/// <param name="transform">The transform function for each IDataRecord.</param>
278-
/// <returns>The results of each transformation.</returns>
279-
protected IEnumerable<T> IterateReaderInternal<T>(Func<IDataRecord, T> transform)
273+
/// <summary>
274+
/// Calls ExecuteNonQuery on the underlying command but sets up a return parameter and returns that value.
275+
/// </summary>
276+
/// <returns>The value from the return parameter.</returns>
277+
public object ExecuteReturn()
278+
{
279+
using (var con = ConnectionFactory.Create())
280+
using (var cmd = con.CreateCommand(Type, Command, Timeout))
281+
{
282+
var c = cmd as TCommand;
283+
if (c == null) throw new InvalidCastException($"Actual command type ({cmd.GetType()}) is not compatible with expected command type ({typeof(TCommand)}).");
284+
AddParams(c);
285+
var returnParameter = c.CreateParameter();
286+
returnParameter.Direction = ParameterDirection.ReturnValue;
287+
c.Parameters.Add(returnParameter);
288+
con.Open();
289+
c.ExecuteNonQuery();
290+
return returnParameter.Value;
291+
}
292+
}
293+
294+
/// <summary>
295+
/// Calls ExecuteNonQuery on the underlying command but sets up a return parameter and returns that value.
296+
/// </summary>
297+
/// <returns>The value from the return parameter.</returns>
298+
public T ExecuteReturn<T>()
299+
=> (T)ExecuteReturn();
300+
301+
/// <summary>
302+
/// Internal reader for simplifying iteration. If exposed publicly could potentially hold connections open because an iteration may have not completed.
303+
/// </summary>
304+
/// <typeparam name="T">The return type of the transform function.</typeparam>
305+
/// <param name="transform">The transform function for each IDataRecord.</param>
306+
/// <returns>The results of each transformation.</returns>
307+
protected IEnumerable<T> IterateReaderInternal<T>(Func<IDataRecord, T> transform)
280308
{
281309
using (var con = ConnectionFactory.Create())
282310
using (var cmd = con.CreateCommand(Type, Command, Timeout))
@@ -408,15 +436,15 @@ public List<T> SkipThenTake<T>(int skip, int take, Func<IDataRecord, T> transfor
408436
/// <summary>
409437
/// Calls ExecuteNonQuery on the underlying command.
410438
/// </summary>
411-
/// <returns>The integer responise from the method.</returns>
439+
/// <returns>The integer response from the method. (Records updated.)</returns>
412440
public int ExecuteNonQuery()
413-
=> Execute(command => command.ExecuteNonQuery());
441+
=> Execute(command => command.ExecuteNonQuery());
414442

415-
/// <summary>
416-
/// Calls ExecuteScalar on the underlying command.
417-
/// </summary>
418-
/// <returns>The varlue returned from the method.</returns>
419-
public object ExecuteScalar()
443+
/// <summary>
444+
/// Calls ExecuteScalar on the underlying command.
445+
/// </summary>
446+
/// <returns>The varlue returned from the method.</returns>
447+
public object ExecuteScalar()
420448
=> Execute(command => command.ExecuteScalar());
421449

422450
/// <summary>

ExpressiveDbCommandBase.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,35 @@ public async Task<T> ExecuteAsync<T>(Func<TCommand, Task<T>> transform)
8383
}
8484
}
8585

86+
/// <summary>
87+
/// Calls ExecuteNonQueryAsync on the underlying command but sets up a return parameter and returns that value.
88+
/// </summary>
89+
/// <returns>The value from the return parameter.</returns>
90+
public async Task<object> ExecuteReturnAsync()
91+
{
92+
using (var con = ConnectionFactory.Create())
93+
using (var cmd = con.CreateCommand(
94+
Type, Command, Timeout))
95+
{
96+
var c = cmd as TCommand;
97+
if (c == null) throw new InvalidCastException($"Actual command type ({cmd.GetType()}) is not compatible with expected command type ({typeof(TCommand)}).");
98+
AddParams(c);
99+
var returnParameter = c.CreateParameter();
100+
returnParameter.Direction = ParameterDirection.ReturnValue;
101+
cmd.Parameters.Add(returnParameter);
102+
await con.OpenAsync();
103+
await c.ExecuteNonQueryAsync();
104+
return returnParameter.Value;
105+
}
106+
}
107+
108+
/// <summary>
109+
/// Calls ExecuteNonQueryAsync on the underlying command but sets up a return parameter and returns that value.
110+
/// </summary>
111+
/// <returns>The value from the return parameter.</returns>
112+
public async Task<T> ExecuteReturnAsync<T>()
113+
=> (T)(await ExecuteReturnAsync());
114+
86115
/// <summary>
87116
/// Asynchronously executes a reader on a command with a handler function.
88117
/// </summary>

0 commit comments

Comments
 (0)