Skip to content

Commit f92afc2

Browse files
committed
Removed GetValue() private helper method
The initial implementation of the extension methods (#f25a063) was a bit too cute with an attempt to centralize the logic. With subsequent efforts to refine this logic (#28948f7, #47ef38d, #adcf6a1, #294b59d) the cracks started to show in this approach. At this point, it is either not used (e.g., for `GetVersion()`), or is providing unnecessary overhead (e.g., by converting `INT` fields to a string value and then back to an `int`). As such, the private `GetValue()` helper was removed, and the final two extension methods were converted to using local methods. The only one that looks even remotely like the original `GetValue()` helper is `GetInteger()`, but it is considerably simpler since the
1 parent a015210 commit f92afc2

1 file changed

Lines changed: 3 additions & 30 deletions

File tree

OnTopic.Data.Sql/SqlDataReaderExtensions.cs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ internal static class SqlDataReaderExtensions {
3333
/// <param name="reader">The <see cref="SqlDataReader"/> object.</param>
3434
/// <param name="columnName">The name of the column to retrieve the value from.</param>
3535
internal static int GetInteger(this SqlDataReader reader, string columnName) =>
36-
GetValue<int>(reader, columnName, Int32.TryParse, -1);
36+
Int32.TryParse(reader.GetValue(reader.GetOrdinal(columnName)).ToString(), out var output)? output : -1;
37+
3738
/*==========================================================================================================================
3839
| METHOD: GET TOPIC ID
3940
\-------------------------------------------------------------------------------------------------------------------------*/
@@ -54,7 +55,7 @@ internal static int GetTopicId(this SqlDataReader reader, string columnName = "T
5455
/// <param name="reader">The <see cref="SqlDataReader"/> object.</param>
5556
/// <param name="columnName">The name of the column to retrieve the value from.</param>
5657
internal static string GetString(this SqlDataReader reader, string columnName) =>
57-
GetValue<string>(reader, columnName, (string source, out string value) => { value = source; return true; }, String.Empty);
58+
reader.GetString(reader.GetOrdinal(columnName));
5859

5960
/*==========================================================================================================================
6061
| METHOD: GET VERSION
@@ -66,33 +67,5 @@ internal static string GetString(this SqlDataReader reader, string columnName) =
6667
internal static DateTime GetVersion(this SqlDataReader reader) =>
6768
reader.GetDateTime(reader.GetOrdinal("Version"));
6869

69-
/*==========================================================================================================================
70-
| METHOD: GET VALUE
71-
\-------------------------------------------------------------------------------------------------------------------------*/
72-
/// <summary>
73-
/// Retrieves a string value by column name.
74-
/// </summary>
75-
/// <param name="reader">The <see cref="SqlDataReader"/> object.</param>
76-
/// <param name="columnName">The name of the column to retrieve the value from.</param>
77-
/// <param name="parser">Function to call in order to convert the string value to a strongly typed value.</param>
78-
/// <param name="defaultValue">The default value, in case <see cref="DBNull"/> is returned.</param>
79-
private static T GetValue<T>(
80-
SqlDataReader reader,
81-
string columnName,
82-
TryParse<string, T, bool> parser,
83-
object defaultValue
84-
) {
85-
Contract.Requires(columnName, nameof(columnName));
86-
var ordinal = reader.GetOrdinal(columnName);
87-
if (reader.IsDBNull(ordinal)) {
88-
return (T)defaultValue;
89-
}
90-
var value = reader.GetValue(ordinal).ToString();
91-
if (parser(value, out var output)) {
92-
return output;
93-
}
94-
return (T)defaultValue;
95-
}
96-
9770
} //Class
9871
} //Namespace

0 commit comments

Comments
 (0)