Skip to content

Commit

Permalink
move the parameter callbacks onto a method that custom implementation…
Browse files Browse the repository at this point in the history
…s can also use
  • Loading branch information
mgravell committed Sep 15, 2014
1 parent 9226076 commit 79e1e3a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
41 changes: 26 additions & 15 deletions Dapper NET40/SqlMapper.cs
Expand Up @@ -73,11 +73,11 @@ internal static CommandDefinition ForCallback(object parameters)
private readonly CommandFlags flags;


internal void FireOutputCallbacks()
internal void OnCompleted()
{
if (parameters is DynamicParameters)
if (parameters is SqlMapper.IParameterCallbacks)
{
((DynamicParameters)parameters).FireOutputCallbacks();
((SqlMapper.IParameterCallbacks)parameters).OnCompleted();
}
}
/// <summary>
Expand Down Expand Up @@ -263,6 +263,17 @@ public interface IParameterLookup : IDynamicParameters
object this[string name] { get; }
}

/// <summary>
/// Extends IDynamicParameters with facitilies for executing callbacks after commands have completed
/// </summary>
public partial interface IParameterCallbacks : IDynamicParameters
{
/// <summary>
/// Invoked when the command has executed
/// </summary>
void OnCompleted();
}

/// <summary>
/// Implement this interface to pass an arbitrary db specific parameter to Dapper
/// </summary>
Expand Down Expand Up @@ -1251,7 +1262,7 @@ private static int ExecuteImpl(this IDbConnection cnn, ref CommandDefinition com
total += cmd.ExecuteNonQuery();
}
}
command.FireOutputCallbacks();
command.OnCompleted();
} finally
{
if (wasClosed) cnn.Close();
Expand Down Expand Up @@ -1532,7 +1543,7 @@ private static IEnumerable<T> QueryImpl<T>(this IDbConnection cnn, CommandDefini
reader.Dispose();
reader = null;

command.FireOutputCallbacks();
command.OnCompleted();
}
finally
{
Expand Down Expand Up @@ -1785,7 +1796,7 @@ partial class DontMap { }
if(finalize)
{
while (reader.NextResult()) { }
command.FireOutputCallbacks();
command.OnCompleted();
}
}
}
Expand Down Expand Up @@ -1856,7 +1867,7 @@ static IEnumerable<TReturn> MultiMapImpl<TReturn>(this IDbConnection cnn, Comman
if (finalize)
{
while (reader.NextResult()) { }
command.FireOutputCallbacks();
command.OnCompleted();
}
}
}
Expand Down Expand Up @@ -3163,7 +3174,7 @@ private static int ExecuteCommand(IDbConnection cnn, ref CommandDefinition comma
cmd = command.SetupCommand(cnn, paramReader);
if (wasClosed) cnn.Open();
int result = cmd.ExecuteNonQuery();
command.FireOutputCallbacks();
command.OnCompleted();
return result;
}
finally
Expand Down Expand Up @@ -3191,7 +3202,7 @@ private static T ExecuteScalarImpl<T>(IDbConnection cnn, ref CommandDefinition c
cmd = command.SetupCommand(cnn, paramReader);
if (wasClosed) cnn.Open();
result =cmd.ExecuteScalar();
command.FireOutputCallbacks();
command.OnCompleted();
}
finally
{
Expand Down Expand Up @@ -3915,12 +3926,12 @@ public partial class GridReader : IDisposable
private IDbCommand command;
private Identity identity;

internal GridReader(IDbCommand command, IDataReader reader, Identity identity, DynamicParameters dynamicParams)
internal GridReader(IDbCommand command, IDataReader reader, Identity identity, SqlMapper.IParameterCallbacks callbacks)
{
this.command = command;
this.reader = reader;
this.identity = identity;
this.dynamicParams = dynamicParams;
this.callbacks = callbacks;
}

#if !CSHARP30
Expand Down Expand Up @@ -4127,7 +4138,7 @@ private IEnumerable<T> ReadDeferred<T>(int index, Func<IDataReader, object> dese
}
private int gridIndex, readCount;
private bool consumed;
private DynamicParameters dynamicParams;
private SqlMapper.IParameterCallbacks callbacks;

/// <summary>
/// Has the underlying reader been consumed?
Expand All @@ -4153,7 +4164,7 @@ private void NextResult()
// need for "Cancel" etc
reader.Dispose();
reader = null;
if (dynamicParams != null) dynamicParams.FireOutputCallbacks();
if (callbacks != null) callbacks.OnCompleted();
Dispose();
}
}
Expand Down Expand Up @@ -4214,7 +4225,7 @@ public static string GetTypeName(this DataTable table)
/// <summary>
/// A bag of parameters that can be passed to the Dapper Query and Execute methods
/// </summary>
partial class DynamicParameters : SqlMapper.IDynamicParameters, SqlMapper.IParameterLookup
partial class DynamicParameters : SqlMapper.IDynamicParameters, SqlMapper.IParameterLookup, SqlMapper.IParameterCallbacks
{
internal const DbType EnumerableMultiParameter = (DbType)(-1);
static Dictionary<SqlMapper.Identity, Action<IDbCommand, object>> paramReaderCache = new Dictionary<SqlMapper.Identity, Action<IDbCommand, object>>();
Expand Down Expand Up @@ -4707,7 +4718,7 @@ internal static class CachedOutputSetters<T>
public static readonly Hashtable Cache = new Hashtable();
}

internal void FireOutputCallbacks()
void SqlMapper.IParameterCallbacks.OnCompleted()
{
foreach (var param in (from p in parameters select p.Value))
{
Expand Down
14 changes: 7 additions & 7 deletions Dapper NET45/SqlMapperAsync.cs
Expand Up @@ -96,7 +96,7 @@ private static async Task<IEnumerable<T>> QueryAsync<T>(this IDbConnection cnn,
buffer.Add((T)func(reader));
}
while (await reader.NextResultAsync().ConfigureAwait(false)) { }
command.FireOutputCallbacks();
command.OnCompleted();
return buffer;
}
else
Expand Down Expand Up @@ -238,7 +238,7 @@ private static async Task<int> ExecuteMultiImplAsync(IDbConnection cnn, CommandD
}
}

command.FireOutputCallbacks();
command.OnCompleted();
}
finally
{
Expand All @@ -257,7 +257,7 @@ private static async Task<int> ExecuteImplAsync(IDbConnection cnn, CommandDefini
{
if (wasClosed) await ((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false);
var result = await cmd.ExecuteNonQueryAsync(command.CancellationToken).ConfigureAwait(false);
command.FireOutputCallbacks();
command.OnCompleted();
return result;
}
finally
Expand Down Expand Up @@ -514,8 +514,8 @@ private static IEnumerable<T> ExecuteReaderSync<T>(IDataReader reader, Func<IDat
yield return (T)func(reader);
}
while (reader.NextResult()) { }
if (parameters is DynamicParameters)
((DynamicParameters)parameters).FireOutputCallbacks();
if (parameters is SqlMapper.IParameterCallbacks)
((SqlMapper.IParameterCallbacks)parameters).OnCompleted();
}
}

Expand Down Expand Up @@ -580,7 +580,7 @@ private async Task NextResultAsync()
// need for "Cancel" etc
reader.Dispose();
reader = null;
if (dynamicParams != null) dynamicParams.FireOutputCallbacks();
if (callbacks != null) callbacks.OnCompleted();
Dispose();
}
}
Expand Down Expand Up @@ -814,7 +814,7 @@ private async static Task<T> ExecuteScalarImplAsync<T>(IDbConnection cnn, Comman
cmd = (DbCommand)command.SetupCommand(cnn, paramReader);
if (wasClosed) await ((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false);
result = await cmd.ExecuteScalarAsync(command.CancellationToken).ConfigureAwait(false);
command.FireOutputCallbacks();
command.OnCompleted();
}
finally
{
Expand Down

0 comments on commit 79e1e3a

Please sign in to comment.