Skip to content

Commit

Permalink
Transfer duplicate code and encapsulate it as a static method
Browse files Browse the repository at this point in the history
  • Loading branch information
zqlovejyc committed Nov 19, 2020
1 parent 9fcbb3b commit d7ad382
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 39 deletions.
15 changes: 2 additions & 13 deletions Dapper/SqlMapper.GridReader.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private Task<IEnumerable<T>> ReadAsyncImpl<T>(Type type, bool buffered)
else
{
var result = ReadDeferred<T>(gridIndex, deserializer.Func, type);
if (buffered) result = result.ToList(); // for the "not a DbDataReader" scenario
if (buffered) result = result?.ToList(); // for the "not a DbDataReader" scenario
return Task.FromResult(result);
}
}
Expand Down Expand Up @@ -212,18 +212,7 @@ private async Task<T> ReadRowAsyncImplViaDbReader<T>(DbDataReader reader, Type t
cache.Deserializer = deserializer;
}

object val = deserializer.Func(reader);

if (val != null)
{
if (val is T)
result = (T)val;
else
{
var convertToType = Nullable.GetUnderlyingType(type) ?? type;
result = (T)Convert.ChangeType(val, convertToType, CultureInfo.InvariantCulture);
}
}
result = ConvertTo<T>(deserializer.Func(reader));

if ((row & Row.Single) != 0 && await reader.ReadAsync(cancel).ConfigureAwait(false)) ThrowMultipleRows(row);
while (await reader.ReadAsync(cancel).ConfigureAwait(false)) { /* ignore subsequent rows */ }
Expand Down
39 changes: 13 additions & 26 deletions Dapper/SqlMapper.GridReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private IEnumerable<T> ReadImpl<T>(Type type, bool buffered)
}
IsConsumed = true;
var result = ReadDeferred<T>(gridIndex, deserializer.Func, type);
return buffered ? result.ToList() : result;
return buffered ? result?.ToList() : result;
}

private T ReadRow<T>(Type type, Row row)
Expand All @@ -181,18 +181,8 @@ private T ReadRow<T>(Type type, Row row)
deserializer = new DeserializerState(hash, GetDeserializer(type, reader, 0, -1, false));
cache.Deserializer = deserializer;
}
object val = deserializer.Func(reader);

if (val != null)
{
if (val is T)
result = (T)val;
else
{
var convertToType = Nullable.GetUnderlyingType(type) ?? type;
result = (T)Convert.ChangeType(val, convertToType, CultureInfo.InvariantCulture);
}
}
result = ConvertTo<T>(deserializer.Func(reader));

if ((row & Row.Single) != 0 && reader.Read()) ThrowMultipleRows(row);
while (reader.Read()) { /* ignore subsequent rows */ }
Expand Down Expand Up @@ -363,22 +353,9 @@ private IEnumerable<T> ReadDeferred<T>(int index, Func<IDataReader, object> dese
{
try
{
var convertToType = Nullable.GetUnderlyingType(effectiveType) ?? effectiveType;
while (index == gridIndex && reader.Read())
{
T result = default;
object val = deserializer(reader);
if (val != null)
{
if (val is T)
result = (T)val;
else
{
result = (T)Convert.ChangeType(val, convertToType, CultureInfo.InvariantCulture);
}
}

yield return result;
yield return ConvertTo<T>(deserializer(reader));
}
}
finally // finally so that First etc progresses things even when multiple rows
Expand Down Expand Up @@ -440,6 +417,16 @@ public void Dispose()
}
GC.SuppressFinalize(this);
}

private static T ConvertTo<T>(object value)
{
if (value is null or DBNull)
return default;
else if (value is T t)
return t;
else
return (T)Convert.ChangeType(value, Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T), CultureInfo.InvariantCulture);
}
}
}
}

0 comments on commit d7ad382

Please sign in to comment.