Skip to content

Commit

Permalink
Use Bcl async interfaces to add async streams to standard 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Medfar-DavidFrancis committed Aug 4, 2021
1 parent c6e436a commit ffb2baf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Dapper.StrongName/Dapper.StrongName.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Title>Dapper (Strong Named)</Title>
<Description>A high performance Micro-ORM supporting SQL Server, MySQL, Sqlite, SqlCE, Firebird etc..</Description>
<Authors>Sam Saffron;Marc Gravell;Nick Craver</Authors>
<TargetFrameworks>net461;netstandard2.0;netstandard2.1;net5.0</TargetFrameworks>
<TargetFrameworks>net461;netstandard2.0;net5.0</TargetFrameworks>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
</PropertyGroup>
Expand All @@ -19,6 +19,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion Dapper/Dapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<PackageTags>orm;sql;micro-orm</PackageTags>
<Description>A high performance Micro-ORM supporting SQL Server, MySQL, Sqlite, SqlCE, Firebird etc..</Description>
<Authors>Sam Saffron;Marc Gravell;Nick Craver</Authors>
<TargetFrameworks>net461;netstandard2.0;netstandard2.1;net5.0</TargetFrameworks>
<TargetFrameworks>net461;netstandard2.0;net5.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Folder Include="Properties\" />
Expand All @@ -16,6 +16,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
</ItemGroup>
</Project>
54 changes: 42 additions & 12 deletions Dapper/SqlMapper.AsyncStream.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NET5_0 || NETSTANDARD2_1
#if NET5_0 || NETSTANDARD2_0
using System;
using System.Collections.Generic;
using System.Data;
Expand Down Expand Up @@ -96,7 +96,10 @@ private static async IAsyncEnumerable<T> StreamAsync<T>(this IDbConnection cnn,
var info = GetCacheInfo(identity, param, command.AddToCache);
bool wasClosed = cnn.State == ConnectionState.Closed;
var cancel = command.CancellationToken;
await using var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader);
#if NET5_0
await
#endif
using var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader);
DbDataReader reader = null;
try
{
Expand Down Expand Up @@ -131,7 +134,10 @@ private static async IAsyncEnumerable<T> StreamAsync<T>(this IDbConnection cnn,
}
finally
{
await using (reader) { /* dispose if non-null */ }
#if NET5_0
await
#endif
using (reader) { /* dispose if non-null */ }
if (wasClosed) cnn.Close();
}
}
Expand Down Expand Up @@ -392,8 +398,14 @@ private static async IAsyncEnumerable<TReturn> MultiMapStreamAsync<TFirst, TSeco
try
{
if (wasClosed) await cnn.TryOpenAsync(command.CancellationToken).ConfigureAwait(false);
await using var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader);
await using var reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, CommandBehavior.SequentialAccess | CommandBehavior.SingleResult, cancel).ConfigureAwait(false);
#if NET5_0
await
#endif
using var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader);
#if NET5_0
await
#endif
using var reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, CommandBehavior.SequentialAccess | CommandBehavior.SingleResult, cancel).ConfigureAwait(false);
if (!command.Buffered) wasClosed = false; // handing back open reader; rely on command-behavior
var results = MultiMapStreamImpl<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn>(null, CommandDefinition.ForCallback(command.Parameters), map, splitOn, reader, identity, true);

Expand Down Expand Up @@ -451,8 +463,14 @@ private static async IAsyncEnumerable<TReturn> MultiMapStreamAsync<TReturn>(this
try
{
if (wasClosed) await cnn.TryOpenAsync(command.CancellationToken).ConfigureAwait(false);
await using var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader);
await using var reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, CommandBehavior.SequentialAccess | CommandBehavior.SingleResult, cancel).ConfigureAwait(false);
#if NET5_0
await
#endif
using var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader);
#if NET5_0
await
#endif
using var reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, CommandBehavior.SequentialAccess | CommandBehavior.SingleResult, cancel).ConfigureAwait(false);
var results = MultiMapAsyncImpl(null, default, types, map, splitOn, reader, identity, true);

var buffer = command.Buffered ? new List<TReturn>() : null;
Expand Down Expand Up @@ -523,12 +541,18 @@ private static async IAsyncEnumerable<TReturn> MultiMapStreamImpl<TFirst, TSecon
{
try
{
await using (ownedReader) { /* dispose if non-null */ }
#if NET5_0
await
#endif
using (ownedReader) { /* dispose if non-null */ }
}
finally
{
ownedCommand?.Parameters.Clear();
await using (ownedCommand) { /* dispose if non-null */ }
#if NET5_0
await
#endif
using (ownedCommand) { /* dispose if non-null */ }
if (wasClosed) cnn.Close();
}
}
Expand Down Expand Up @@ -590,16 +614,22 @@ private static async IAsyncEnumerable<TReturn> MultiMapAsyncImpl<TReturn>(this I
{
try
{
await using (ownedReader) { /* dispose if non-null */ }
#if NET5_0
await
#endif
using (ownedReader) { /* dispose if non-null */ }
}
finally
{
ownedCommand?.Parameters.Clear();
await using (ownedCommand) { /* dispose if non-null */ }
#if NET5_0
await
#endif
using (ownedCommand) { /* dispose if non-null */ }
if (wasClosed) cnn.Close();
}
}
}
}
}
#endif // NET5_0
#endif // NET5_0 || NETSTANDARD2_0
4 changes: 2 additions & 2 deletions Dapper/SqlMapper.GridReader.AsyncStream.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NET5_0 || NETSTANDARD2_1
#if NET5_0 || NETSTANDARD2_0
using System;
using System.Collections.Generic;
using System.Data;
Expand Down Expand Up @@ -97,4 +97,4 @@ private async IAsyncEnumerable<T> ReadStreamAsync<T>(int index, Func<IDataReader
}
}
}
#endif // NET5_0 || NETSTANDARD2_1
#endif // NET5_0 || NETSTANDARD2_0

0 comments on commit ffb2baf

Please sign in to comment.