-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ExecuteReaderAsync overloads that return Task<DbDataReader> (#1295)
- Loading branch information
Showing
3 changed files
with
96 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Dapper | ||
{ | ||
internal static class Extensions | ||
{ | ||
/// <summary> | ||
/// Creates a <see cref="Task{TResult}"/> with a less specific generic parameter that perfectly mirrors the | ||
/// state of the specified <paramref name="task"/>. | ||
/// </summary> | ||
internal static Task<TTo> CastResult<TFrom, TTo>(this Task<TFrom> task) | ||
where TFrom : TTo | ||
{ | ||
if (task is null) throw new ArgumentNullException(nameof(task)); | ||
|
||
if (task.Status == TaskStatus.RanToCompletion) | ||
return Task.FromResult((TTo)task.Result); | ||
|
||
var source = new TaskCompletionSource<TTo>(); | ||
task.ContinueWith(OnTaskCompleted<TFrom, TTo>, state: source, TaskContinuationOptions.ExecuteSynchronously); | ||
return source.Task; | ||
} | ||
|
||
private static void OnTaskCompleted<TFrom, TTo>(Task<TFrom> completedTask, object state) | ||
where TFrom : TTo | ||
{ | ||
var source = (TaskCompletionSource<TTo>)state; | ||
|
||
switch (completedTask.Status) | ||
{ | ||
case TaskStatus.RanToCompletion: | ||
source.SetResult(completedTask.Result); | ||
break; | ||
case TaskStatus.Canceled: | ||
source.SetCanceled(); | ||
break; | ||
case TaskStatus.Faulted: | ||
source.SetException(completedTask.Exception.InnerExceptions); | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters